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

I am trying to redirect my request to an error page when an error occurs while creating an excel sheet. Here is the sample code below. I'm not sure why it is not sent to the error page when an exception occurs, it displays a blank page, but it will not necessarily be on my error page.

@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
source share
4 answers

I'm not sure, but I assume that you did not set any rendering option when redirecting to the error page.

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

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

I use such redirects with actionResponse , but it should work with resourceResponse as well ...

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

create renderURL using response.createRenderURL() . If a request is launched using this URL, this will result in a request / response to the visa (or a request for an action that can access this method).

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

0
source

I'm not sure if 100% of this will work in resourcePhase, but you can try

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

I had a similar problem. It works -

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

Perhaps this is not a forwarding, because the answer has already been made, because you wrote something in it. This may explain why the inclusion of jobs and transitions does not.

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

0
source

Source: https://habr.com/ru/post/1493546/


All Articles