How to make the ResourceResponse to forward the request to error page in liferay portlet

时间:2022-02-14 16:58:38

I am trying to forward my request to error page when error occurs during generating excel sheet. Here is sample code below. I am not sure why it is not getting forwarded to error page when the exception is thrown, it is displaying blank page but not going to my error page for sure.`

我正在尝试在生成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 个解决方案

#1


0  

Im not sure but my guess would be that you didnt set any render parameter when redirecting to your error page.

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

Try this and see if it helps in any way (you can place it instead of the line with dispatcher):

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

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

I am using this kind of redirects with actionResponse but it should work with resourceResponse as well...

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

EDIT: resource response does not contain setRenderParameter method, but zou can try to use the following approach:

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

create renderURL using response.createRenderURL(). If a request is triggered using this URL, it will result in render request/response (or action request which can access that method).

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

The problem is, that you are trying to redirect to another page during resource phase of the portlet (render phase in not called during this phase).

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

#2


0  

I'm not 100% sure this would work in the resourcePhase, but you could try

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

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

#3


0  

I had a similar issue. This works -

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

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

#4


0  

Maybe it is not forwarding because the response has already been committed because you have written something in it. That could explain why include works and forward doesn't.

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

You can check whether the response has already been committed using resourceResponse.isCommitted() in your catch block.

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

#1


0  

Im not sure but my guess would be that you didnt set any render parameter when redirecting to your error page.

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

Try this and see if it helps in any way (you can place it instead of the line with dispatcher):

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

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

I am using this kind of redirects with actionResponse but it should work with resourceResponse as well...

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

EDIT: resource response does not contain setRenderParameter method, but zou can try to use the following approach:

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

create renderURL using response.createRenderURL(). If a request is triggered using this URL, it will result in render request/response (or action request which can access that method).

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

The problem is, that you are trying to redirect to another page during resource phase of the portlet (render phase in not called during this phase).

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

#2


0  

I'm not 100% sure this would work in the resourcePhase, but you could try

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

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

#3


0  

I had a similar issue. This works -

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

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

#4


0  

Maybe it is not forwarding because the response has already been committed because you have written something in it. That could explain why include works and forward doesn't.

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

You can check whether the response has already been committed using resourceResponse.isCommitted() in your catch block.

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