Spring portlet将jsp响应作为字符串

时间:2013-08-20 16:36:14

标签: java spring dispatch spring-portlet-mvc

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

1 个答案:

答案 0 :(得分:0)

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

    };