struts 2在调用堆栈中找不到名为[excelStream]的java.io.InputStream [英] struts 2 Can not find a java.io.InputStream with the name [excelStream] in the invocation stack

查看:1027
本文介绍了struts 2在调用堆栈中找不到名为[excelStream]的java.io.InputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

excelStreamI正在尝试下载一个excel文件。

excelStreamI am trying to download an excel file.

在我的Action类中

In my Action class

public class ActivityTrackerExlReportAction extends BaseAction 
{
private  InputStream excelStream;
private UserMasterDTO userMasterDTO;

public InputStream getExcelStream() 
{
 return excelStream;
}

public void setExcelStream(InputStream excelStream) {
this.excelStream = excelStream;
}

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
WorkbookSettings wbSettings = new WorkbookSettings();

try
{
  response.setHeader("Content-Disposition", "attachment; filename=/timesheet.xls");
  wbSettings.setLocale(new Locale("en", "EN"));
  WritableWorkbook workbook = Workbook.createWorkbook(outputStream, wbSettings);
  workbook.createSheet("Report", 0);
  WritableSheet excelSheet = workbook.getSheet(0);
  service.createLabel(excelSheet);
  service.createContent(excelSheet);  

  workbook.write();

  ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

  setExcelStream(inputStream);

  workbook.close();

  outStream.flush();

  outStream.close();
}
catch(Exception e)
{
}
finally
{
// outStream.close();
 }
 return "generateReport
}

我的 struts.xml 包含:

<result type="stream" name="generateReport">
 <param name="contentType">"application/vnd.ms-excel"</param>
 <param name="inputName">excelStream</param>
 <param name="bufferSize">1024</param>
</result>

我正在使用 JXL 来创建和编写一个Excel表格,为什么我会收到错误以及如何解决错误?找不到 java.io.InputStream 在调用堆栈中的名称为 [excelStream]

I am using JXL to create and write an Excel sheet. Why am i getting the error and how to get out of it? Can not find a java.io.InputStream with the name [excelStream] in the invocation stack

我是StackTraces:

My are Stacktraces:


java.lang.IllegalArgumentException:找不到java.io.InputStream在调用堆栈中命名[excelStream]。检查为此操作指定的标签。

java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [excelStream] in the invocation stack. Check the tag specified for this action.

org.apache.struts2.dispatcher.StreamResult.doExecute(StreamRes ult.java:237)

org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:237)

org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
.......

org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186) .......


推荐答案

编辑

在新发布的代码中,您正在执行两个不好的事情:

In the new posted code, you are doing two "bad" things:

1)您正在吞下异常这真的是错误的;

放一个

e.printStackTrace();

在您的

catch(Exception e){}

阻止,你会看到Exception可能是被扔,抓住并没有显示;

block, and you will see the Exception that is probably being throwed, catched and not showed;

2)即使你得到一个异常 99.9%发生了什么)这将导致Struts2 Stream的结果试图达到未被初始化的excelStream变量(由于异常)。

2) You are returning the same result even if you get an Exception, (that is 99.9% what is happening); this will result in Struts2 Stream result trying to reach the excelStream variable that was never initialized (because of the exception).

如果出现错误,您应该返回一个全局(或本地)错误结果类型,JSP显示错误,而不是Stream结果类型。

In case of error, you should return an global (or local) error result type, with a JSP showing the error, instead of a Stream result type.

打印异常,更正代码,然后一切都会正常:)

Print the exception, correct the code, and then everything will be alright :)

PS:请避免直接在回复中写入,使用Stream结果类型中的contentDisposition。

P.S: Please avoid writing in the response directly, use contentDisposition from the Stream result type.

错误是

 <param name="inputName">excelstream</param>

应该是

 <param name="inputName">excelStream</param>



<

Always use camelCase (as you did in the Action, but not in struts config).

显然,ContentDisposition应该被删除,或设置一个适当的值,如

And obviously contentDisposition should be removed, or set with a proper value, like

 <param name="contentDisposition">attachment; filename="myExcel.xls"</param>

这篇关于struts 2在调用堆栈中找不到名为[excelStream]的java.io.InputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆