【问题标题】:Spring portlet get jsp response as stringSpring portlet 以字符串形式获取 jsp 响应
【发布时间】:2013-08-20 16:36:14
【问题描述】:

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

【问题讨论】:

  • 我更新了代码,但仍然不走运,因为父响应中仍然缺少某些内容,并且当我调试代码时 getwritter 没有被调用。

标签: java spring dispatch spring-portlet-mvc


【解决方案1】:

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

    };

【讨论】:

  • 尝试在上面的代码中覆盖getPortletOutputStream()。我不知道为什么它对你不起作用,这对我在 Liferay/Tomcat 服务器上非常有效。
  • 我尝试覆盖 getPortletOutputStream 但我不知道为什么不起作用。
  • 能否请您复制粘贴您的控制器代码,可能是我遗漏了一些东西,我可以从中找到。
猜你喜欢
  • 2017-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-20
相关资源
最近更新 更多