2013-07-25 46 views
4

我正在嘗試在生成excel表單期間發生錯誤時將請求轉發到錯誤頁面。以下是示例代碼。我不知道爲什麼它沒有得到轉發時拋出異常錯誤頁面,它顯示的是空白頁,但不會去我的錯誤頁面sure.`如何使ResourceResponse將請求轉發到liferay portlet中的錯誤頁面

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

回答

0

林不知道,但我的猜測是當你重定向到你的錯誤頁面時你沒有設置任何渲染參數。

試試這個,看看它是否有助於以任何方式(你可以把它,而不是與調度行):

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

我使用這種重定向與ActionResponse的,但它應該工作resourceResponse,以及...

編輯:資源響應不包含的setRenderParameter方法,但鄒可以嘗試用下面的辦法:

使用response.createRenderURL()創建renderURL。如果使用此URL觸發請求,則會導致呈現請求/響應(或可以訪問該方法的操作請求)。

問題是,您正試圖在portlet的資源階段期間重定向到另一個頁面(在此階段未呈現階段)。

+0

我認爲「setRenderParameter」方法不存在於「ResourceResponse」接口下。是的,它出現在你用在你的案例中的「ActionResponse」中 –

+0

我編輯了我的答案,也許這將有助於弄清楚該怎麼做..我認爲調用渲染請求並在處理此方法之後獲取渲染階段將使它工作... – Smajl

+0

謝謝你的幫助Smajil幸運的男孩。我通過在dispatcher.forward(request,response);'之前添加'dispatcher.include(request,response);'來實現它。儘管我沒有獲得與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

但是這可能會做一個重定向,而不是一個前鋒。 – AdrianRM

0

也許它不轉發,因爲響應已經提交,因爲您已經寫了一些東西。這可以解釋爲什麼包括作品和轉發沒有。

你可以檢查你的catch塊是否已經使用resourceResponse.isCommitted()提交了響應。

相關問題