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

4 回答 4

0

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

PortletURL renderUrl = resourceResponse.createRenderURL();  
renderUrl.setParameter("renderException", ex.toString());   
resourceResponse.addProperty("Location", renderUrl.toString());
于 2014-01-16T05:47:33.777 回答
0

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

试试这个,看看它是否有任何帮助(你可以把它放在调度员的线上):

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

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

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

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

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

于 2013-07-26T08:05:53.077 回答
0

我不是 100% 确定这会在资源阶段工作,但你可以尝试

com.liferay.portal.kernel.servlet.SessionErrors.add(request, "your Error message here");
于 2013-07-30T12:15:00.240 回答
0

也许它没有转发,因为响应已经提交,因为你在里面写了一些东西。这可以解释为什么 include 有效而 forward 无效。

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

于 2014-03-27T12:19:26.023 回答