struts.xml执行得不好?

时间:2017-02-03 18:44:57

标签: java xml jsp struts2

或者,至少,我认为问题在于它...... 在下面的第一行中,日志表明流不为空 在第二个说,在错误堆栈跟踪的开头,是null:

01/02 07:30:07| INFO [http-apr-10080-exec-6] (BillAction.java:704) - completeExport() - inputStream=java.io.ByteArrayInputStream@11d1921
01/02 07:30:07| ERROR [http-apr-10080-exec-6] (CommonsLogger.java:34) - Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name="inputName"> tag specified f
or this action.
01/02 07:30:07| ERROR [http-apr-10080-exec-6] (CommonsLogger.java:38) - Exception occurred during processing request: Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Chec
k the <param name="inputName"> tag specified for this action.
java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
        at org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:237)
        at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
        at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:367)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:271)
        at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
        at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
        at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
        at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
        at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
        at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
        at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
etc... etc...

这是产生这种结果的代码:

struts.xml中

<action name="exportExcelBill" 
    class="it.colmenjv.dlq.actions.BillAction"
    method="exportBillToExcel">
    <interceptor-ref name="loginStack" />
    <result name="login">timeout.jsp</result>
    <result name="success">jsp/common/intermeanExcel.jsp</result>
</action>
<action name="completeExport" 
    class="it.colmenjv.dlq.actions.BillAction"
    method="completeExport">
    <interceptor-ref name="loginStack" />
    <result name="login">timeout.jsp</result>
    <result name="success" type="stream">
        <param name="contentDisposition">attachment;filename="${reportFile}"</param>
        <param name="contentType">application/vnd.ms-excel</param>
        <param name="inputName">inputStream</param>
        <param name="bufferSize">1024</param>
    </result>
</action>

exportBillToExcel():

public String exportBillToExcel() {
    XSSFWorkbook myWorkBook = new XSSFWorkbook();
    XSSFSheet mySheet = myWorkBook.createSheet(categ);
    try {

    ... etc. body of the method ... etc...

    }
    catch (Exception e) {
        e.printStackTrace();
    }
===>    session.put("tmpstream", inputStream);
    return SUCCESS;
}

intermeanExcel.jsp:

<head>
    <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="js/common/common.js"></script>
</head>

<body>
    <%@ include file="./warningwait.jsp" %>

     <script> 
         $(document).ready(function() {
                                        closeBoxWait(previous_boxWait);
                                        window.location = '<s:url namespace="/" action="completeExport"/>';
                                    }); 
    </script>   
</body>

completeExport():

public String completeExport() {
    inputStream = (InputStream)session.get("tmpstream");
    logger.info("completeExport() - inputStream="+inputStream);
    return SUCCESS;
}

并且在类中自然会出现inputStream声明以及get和set方法。

我错了吗?

2 个答案:

答案 0 :(得分:0)

你能展示你的getInputStream()方法吗?也许有一些错误。您不需要setInputStream()方法来下载文件。

BTW:会话中输入流的“停放”有点尴尬。我不确定这是否有效。我想你必须在completeExport()方法中创建inputStream。

答案 1 :(得分:0)

尝试在struts.xml中将结果类型定义为流

<action name="exportExcelBill" 
    class="it.colmenjv.dlq.actions.BillAction"
    method="exportBillToExcel">
    <interceptor-ref name="loginStack" />
    <result type = "stream">
        <param name="inputName">tmpstream</param>
    </result>
    <result name="login">timeout.jsp</result>
    <result name="success">jsp/common/intermeanExcel.jsp</result>
</action>

无需将tmpstream对象放入java类的会话中。由于我们已经在结果中定义了tmpstream,因此可以在jsp中访问它。