How to define the stream Result annotation?

和自甴很熟 提交于 2019-12-17 20:59:26

问题


I need to export the excel sheet, so calling action class method through Ajax call from dialog window. I have excel icon when I click the icon calling method and processing the backend data and finally going to execute the below code and return to result set to download the excel sheet.

jQuery Ajax calling:

function callajax() {
     jQuery.ajax({
     url : '<s:url action="part" method="export"/>',

Result set method:

@Result(name="success",type=StreamResult.class,value="",params={"inputName","inputStream"})

Java code:

public String method {
-----------------
method to call backend...
--------------------------
httpServletResponse.setContentType("application/vnd.ms-excel");
String filename = filters.getPeriod() + "_" +filters.getRegion() ;
httpServletResponse.setHeader("Content-disposition",
"attachment; filename="+filename+".xls");
ServletOutputStream outputStream = httpServletResponse.getOutputStream();
builder.build(method1, method2, outputStream);
outputStream.flush();
return SUCCESS; }

In the build method has creating workbook

WorkbookSettings workbookSettings = new WorkbookSettings();
workbookSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook =
Workbook.createWorkbook(outputStream, workbookSettings);

and setting the all the values in sheet. Please advise.

trying as your advise :

I'm using code behind , so result type on class name :

@Result(name="success",type=StreamResult.class,value="",params = {"contentType","application/octet-stream","inputName","inputStream","bufferSize","1024","contentDisposition","attachment;filename=\"${filename}\""})


public String method {
    -----------------
    method to call backend...
    --------------------------
String filename =   filters.getRegion() + "_" +
                filters.getCurrency();
File file = new File(filename);
inputStream = new FileInputStream(file); //getting error "java.io.FileNotFoundException:file "
builder.buildXL(OBJ1, OBJ2, file);
return SUCCESS;
}

buildxl(obj1,obj2,File file)
{
WorkbookSettings workbookSettings = new WorkbookSettings();
    workbookSettings.setLocale(new Locale("en", "EN"));
    WritableWorkbook workbook =
    Workbook.createWorkbook(file, workbookSettings);

-----
------
workbook.write(); 
workbook.close();
}

params values :

params = 
{"contentType","application/octet-stream",
"inputName","inputStream",
"bufferSize","1024",
"contentDisposition","attachment;filename=\"${filename}\""})

log :

2013-07-18 10:25:33,453 ERROR         org.apache.struts2.dispatcher.StreamResult - Can not find a java.io.InputStream with the name [] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
2013-07-18 10:25:33,453 ERROR       org.apache.struts2.rest.RestActionInvocation - Exception processing the result.
java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [] 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 org.apache.struts2.rest.RestActionInvocation.executeResult(RestActionInvocation.java:241)
    at org.apache.struts2.rest.RestActionInvocation.processResult(RestActionInvocation.java:198)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:146)
    at com.opensymphony.xwork2.DefaultActionProxy.execute(DefaultActionProxy.java:147)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:510)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:74)
    at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
    at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:74)
    at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:74)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3288)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3254)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
    at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2163)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2089)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2074)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1513)
    at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
2013-07-18 10:25:33,499 DEBUG       org.apache.struts2.rest.RestActionInvocation - No result returned for action at null

ClasspathPackageProvider

org.apache.struts2.config.ClasspathPackageProvider - Adding parmeter[contentType:application/octet-stream] to result.
org.apache.struts2.config.ClasspathPackageProvider - Adding parmeter[inputName:inputStream] to result.
org.apache.struts2.config.ClasspathPackageProvider - Adding parmeter[bufferSize:1024] to result.
org.apache.struts2.config.ClasspathPackageProvider - Adding parmeter[contentDisposition:attachment;filename="${filename}"] to result.

回答1:


Return result as a binary stream

private InputStream inputStream;
private String filename; 

//getters here
public InputStream getInputStream() {
    return inputStream;
}

public String getFilename() {
  return filename;
}

@Result(name="success", type=StreamResult.class, value="inputStream", params={"contentType","application/octet-stream", "contentDisposition", "attachment;filename=\"${filename}\"", "inputName", "inputStream"})

String filename = filters.getPeriod() + "_" +filters.getRegion() ;
File file = new File(filename);
inputStream = new FileInputStream(file);
return SUCCESS;


来源:https://stackoverflow.com/questions/17660781/how-to-define-the-stream-result-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!