如何使ResourceResponse将请求转发到liferay portlet中的错误页面

问题描述 投票:4回答:4

我正在尝试在生成Excel工作表时发生错误时将我的请求转发到错误页面。以下是示例代码。我不确定为什么在抛出异常时它没有被转发到错误页面,它显示空白页面但是没有进入我的错误页面肯定。

        @ResourceMapping("xyz")
    public void generateExcelExport(ResourceRequest request, ResourceResponse response)  {
        try {
            //Do all the excel related logic
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setProperty("Content-Disposition", "attachment; filename=\"" + XYZ + "\"");
            workbook.write(response.getPortletOutputStream());
        } catch (Exception e) {
            response.setProperty("Content-Disposition", "inline" );
            response.setContentType("text/html");
            PortletRequestDispatcher dispatcher = request.getPortletSession().getPortletContext().getRequestDispatcher("/WEB-INF/views/html/jsp/error.jsp");
            try {
                dispatcher.forward(request, response);              
            } catch (Exception e1) {                
                log.error("Unable to forward the request from the portlet", e1);
            } 
        } }
java model-view-controller portlet liferay-6 forward
4个回答
0
投票

我不确定,但我的猜测是,在重定向到错误页面时没有设置任何渲染参数。

尝试这个,看看它是否有任何帮助(你可以放置它而不是调度程序的行):

response.setRenderParameter("jspPage", "/WEB-INF/views/html/jsp/error.jsp");

我正在使用actionResponse这种重定向,但它也应该与resourceResponse一起使用...

编辑:资源响应不包含setRenderParameter方法,但您可以尝试使用以下方法:

使用response.createRenderURL()创建renderURL。如果使用此URL触发请求,则会生成呈现请求/响应(或可以访问该方法的操作请求)。

问题是,您正尝试在portlet的资源阶段重定向到另一个页面(在此阶段未调用渲染阶段)。


0
投票

我不是100%肯定这可以在resourcePhase中工作,但你可以试试

com.liferay.portal.kernel.servlet.SessionErrors.add(request, "your Error message here");

0
投票

我有一个类似的问题。这工作 -

PortletURL renderUrl = resourceResponse.createRenderURL();  
renderUrl.setParameter("renderException", ex.toString());   
resourceResponse.addProperty("Location", renderUrl.toString());

0
投票

也许它没有转发,因为响应已经被提交,因为你已经写了一些东西。这可以解释为什么包括作品和前进不包括。

您可以使用catch块中的resourceResponse.isCommitted()检查是否已提交响应。

© www.soinside.com 2019 - 2024. All rights reserved.