gpt4 book ai didi

java - 从 Liferay 的 JSF Portlet 显示 pdf 时出现问题

转载 作者:行者123 更新时间:2023-12-02 00:52:44 24 4
gpt4 key购买 nike

我使用 liferay 5.2 和 jsf-portlet。

我想在页面上按一个按钮来生成一个 PDF。在 ManagedBean 中,我构建了 pdf,我想在响应中显示它。

在名为 outputStreamByteArrayOutputStream 中,我使用 JasperReport 构建了 pdf。

我写:

   PortletResponse portletResponse = (PortletResponse)externalCtx.getResponse(); 
HttpServletResponse httpResponse = PortalUtil.getHttpServletResponse(portletResponse);

ServletOutputStream out = httpResponse.getOutputStream();
String filename="Pdf" + System.currentTimeMillis()+".pdf";
httpResponse.reset();

httpResponse.setContentType("application/pdf");
httpResponse.setHeader("Content-Disposition", "attachment; filename=\""+ filename + "\"");
httpResponse.setContentLength(outputStream.size());

outputStream.writeTo(out);

out.flush();
out.close();

我没有看到任何输出!在 jboss 日志中我读到: IllegaStateException....

出了什么问题?

日志

        11:03:19,716 INFO  [STDOUT] 11:03:19,716 ERROR [IncludeTag] Current URL /web/organo-di-governo/datawarehouse?p_p_id=1_WAR_Portlet_Datawarehouse_INSTANCE_D7s7&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-2&p_p_col_count=1&_1_WAR_Portlet_Datawarehouse_INSTANCE_D7s7_com.sun.faces.portlet.VIEW_ID=%2Fview.xhtml&_1_WAR_Portlet_Datawarehouse_INSTANCE_D7s7_com.sun.faces.portlet.NAME_SPACE=_1_WAR_Portlet_Datawarehouse_INSTANCE_D7s7_ generates exception: null
11:03:19,717 INFO [STDOUT] 11:03:19,717 ERROR [IncludeTag] java.lang.IllegalStateException
at com.liferay.portal.servlet.filters.strip.StripResponse.getWriter(StripResponse.java:85)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:326)
at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:342)
at org.apache.jasper.runtime.JspWriterImpl.print(JspWriterImpl.java:468)
at com.liferay.taglib.util.ThemeUtil.includeVM(ThemeUtil.java:208)
at com.liferay.taglib.util.ThemeUtil.include(ThemeUtil.java:68)
at com.liferay.taglib.util.IncludeTag.doEndTag(IncludeTag.java:59)
at org.apache.jsp.html.common.themes.portal_jsp._jspx_meth_liferay_002dtheme_005finclude_005f1(portal_jsp.java:816)
at org.apache.jsp.html.common.themes.portal_jsp._jspx_meth_c_005fotherwise_005f0(portal_jsp.java:788)
at org.apache.jsp.html.common.themes.portal_jsp._jspService(portal_jsp.java:724)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
11:03:19,718 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException



11:03:19,719 ERROR [[Main Servlet]] Servlet.service() for servlet Main Servlet threw exception
java.lang.IllegalStateException
at com.liferay.portal.servlet.filters.strip.StripResponse.getWriter(StripResponse.java:85)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)



11:03:19,722 INFO [STDOUT] 11:03:19,720 ERROR [OpenSSOFilter] org.apache.jasper.JasperException: java.lang.IllegalStateException
org.apache.jasper.JasperException: java.lang.IllegalStateException
at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:409)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)




11:03:19,722 INFO [STDOUT] n.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


Caused by: java.lang.IllegalStateException
at com.liferay.portal.servlet.filters.strip.StripResponse.getWriter(StripResponse.java:85)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)

最佳答案

执行此操作的唯一合适方法是使用 JSR-286 规范的“RESOURCE”阶段。目前在您的代码中,您处于“RENDER”阶段,该阶段强制只允许“text/html”编码。

在普通 JSP 中,标签看起来像这样:

<portlet:resourceURL id="/super-cool.pdf" />

然后在您的 portlet 类中,您实现了serveResource(ResourceRequest req, ResourceResponse res) 方法,并读取并调用ResourceRequest 的req.getResourceId() 方法,并调用您的业务逻辑(创建您的PDF),然后将其写入输出流。

在 JSF 中,我不完全确定这是否受支持,这意味着唯一的方法是将任务委托(delegate)给 servlet 调用。查看 JSF 以查看是否实现了 JSR-286 资源处理。

关于java - 从 Liferay 的 JSF Portlet 显示 pdf 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2361765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com