4

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.`

        @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);
            } 
        } }
stack user1
  • 116
  • 1
  • 10

4 Answers4

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...

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

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).

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).

Smajl
  • 7,555
  • 29
  • 108
  • 179
  • i think "setRenderParameter" method is not present under "ResourceResponse" interface. Yes it is present in "ActionResponse" which you used in your case – Laxman Rana Jul 26 '13 at 08:52
  • I edited my answer, perhaps it will help to figure out what to do.. I think that calling render request and getting to render phase after processing this method would make it work... – Smajl Jul 26 '13 at 09:09
  • Thanks for your help Smajil & Lucky Boy. I made it work by adding `dispatcher.include(request, response);` before `dispatcher.forward(request, response);`. Although I don't get the same look & feel as that of portlet but I can for now at least show some User friendly Error message. – stack user1 Jul 26 '13 at 21:50
  • You should post your answer and mark it as answered :-) Glad you make it work anyway! – Smajl Jul 29 '13 at 07:28
  • i tried to recreate your steps for the same problem i have but i get org.apache.jasper.JasperException: javax.servlet.jsp.JspException: null any idea? – naoru Oct 02 '13 at 19:31
0

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

com.liferay.portal.kernel.servlet.SessionErrors.add(request, "your Error message here");
yannicuLar
  • 3,083
  • 3
  • 32
  • 50
0

I had a similar issue. This works -

PortletURL renderUrl = resourceResponse.createRenderURL();  
renderUrl.setParameter("renderException", ex.toString());   
resourceResponse.addProperty("Location", renderUrl.toString());
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
sarM
  • 1
  • 1
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.

csierra
  • 1,010
  • 10
  • 8