0

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);
        }
    }
4

1 回答 1

0

在您的代码中,输出仅由 one 消耗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();
        }

    };
于 2013-08-22T14:16:21.070 回答