springportlet以字符串形式获取jsp响应



Spring portlet JSP,发出ajax请求,并在控制器中尝试获取JSP页面,以便我可以传递并生成pdf输出。

但问题是没有得到任何字符串数据,但在jsp页面上返回了html内容,请检查代码如下进行

@Controller("exportSummaryController")
@RequestMapping(value = "VIEW")
public class ExportSummaryController implements PortletConfigAware  {
    @ResourceMapping("exportAccRequest")
    public void accountRollupAction(@RequestParam("accNum") String accNum, 
        @RequestParam("sourceId") String sourceId, @RequestParam("serviceId") String serviceId, 
        @RequestParam("summaryExport") String strExport, ResourceRequest request, ResourceResponse response) throws Exception {
        //processing data
        ResourceResponseWrapper responseWrapper = new ResourceResponseWrapper(response) {
            private final StringWriter sw = new StringWriter();
            @Override
            public PrintWriter getWriter() throws IOException {
                return new PrintWriter(sw);
            }
            @Override
    public OutputStream getPortletOutputStream() throws IOException {
                return(new StringOutputStream(sw));
            }
            @Override
            public String toString() {
                return sw.toString();
            }
        };
        portletConfig.getPortletContext().getRequestDispatcher("/WEB-INF/jsp/account_summary.jsp").include(request, responseWrapper);
        String content = responseWrapper.toString();
        System.out.println("Output : " + content); // here i found empty output on command line but output is returned to jsp page.
    }    
}
public class StringOutputStream extends OutputStream {
        private StringWriter stringWriter;
        public StringOutputStream(StringWriter stringWriter) {
            this.stringWriter = stringWriter;
        }
        public void write(int c) {
            stringWriter.write(c);
        }
    }

在代码中,输出仅由一个OutputStream承担。

试试这个,

ResourceResponseWrapper responseWrapper = new ResourceResponseWrapper(response) {
        private final StringWriter sw = new StringWriter();
        @Override
        public PrintWriter getWriter() throws IOException {
            return new PrintWriter(sw){
                @Override
                public void write(String s, int off, int len)
                {
                    try
                    {   sw.write(s, off, len);
                        response.getWriter().write(s, off, len);
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            };
        }

        @Override
        public String toString() {
            return sw.toString();
        }
    };

最新更新