2011-08-04 68 views
0

我有一個portlet,它有一個commandLink。在commandLink的actionListner上,我調用了backingBean。actionResponse.sendRedirect(url)不起作用

支持bean有以下代碼。

ActionResponse actionResponse = (ActionResponse) LiferayFacesContext.getInstance(). 
    getExternalContext().getResponse(); 
actionResponse.sendRedirect("http://localhost:8080/web/guest/pageName"); 

我有一個重定向到另一個portlet頁面。這兩個portlet都包含在一場戰爭中。

這種重定向不能正常工作,它給: classCastException. can't clast to ActionResponse

我也試過,

ActionResponse actionResponse= (ActionResponse) FacesContext.getCurrentInstance(). 
    getExternalContext().getResponse(); 
actionResponse.sendRedirect("http://localhost:8080/web/guest/pageName"); 

它拋出ClassCastException. Can not cast to ActionResponse

異常堆棧跟蹤:

的Java。 lang.ClassCastException:com.liferay.portle t.ResourceResponseImpl不能轉換爲javax.portlet.ActionResponse 在com.brightsky.action.IPCActionBackingBean.addIPCAction(IPCActionBackingBean.java:63) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本機方法) 在sun.reflect.NativeMethodAccessorImpl .invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.jboss.el .util.ReflectionUtil.invokeMethod(ReflectionUtil.java:328) at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:341) at org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java :58) at org.jboss.el.parser.AstValue.invok e(AstValue.java:96) at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102) at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:144) 在javax.faces.event.ActionEvent.processListener(ActionEvent.java:84) 在javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java: 773) 在javax.faces.component.UICommand.broadcast(UICommand.java:296) 處javax.faces.component.UIViewRoot.processApplication javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781) ( UIViewRoot.java:1246) at com.sun.faces.lifecycle.InvokeApplicationPhase.exe cute(InvokeApplicationPhase.java:77) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114) at org.portletfaces.bridge.BridgeImpl.doFacesRequest(BridgeImpl.java:513) at org.portletfaces.bridge.GenericFacesPortlet.serveResource(GenericFacesPortlet.java:131) at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java: 119) at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:71) at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:92) at javax。 servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internal DoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain。java:206)

注意:我使用Liferay & Icefaces。

謝謝。

回答

1

我曾經有過與Spring和Liferay的我的最後一個項目工作使用actionResponse.sendRedirect()類似的問題,但春天給了我一個更加清晰的異常

Set render parameter has already been called.

這種方法actionReponse.sendRedirect()在Portlet生命週期中相當受限制。根據Portlet API,它不能被後任何ActionResponse的接口的以下方法已被稱爲調用:

  • 的setPortletMode
  • setWindowState
  • 的setRenderParameter
  • setRenderParameters
  • removePublicRenderParamter

很可能您的portlet框架將Por設置爲tletMode和RenderParameter在你編碼之前的某個時刻。我知道Spring在上下文中聲明與portlet相關的bean時會這樣做(儘管最初的想法是通過DI使開發人員的生活更加簡單)。如果您無法在現有portlet框架基礎架構中修復此問題。可能的解決方案是編寫自己的javax.portlet.filter,檢查操作名稱,並正確調用sendRedirect()。

+0

嗨,感謝您的回覆。實際上,我在這一行上得到了類轉換異常ActionResponse actionResponse =(ActionResponse)FacesContext.getCurrentInstance()。getExternalContext()。getResponse();它給出,無法投射到ActionResponse。如果我做了類似FacesContext.getCurrentInstance()。getExternalContext()。redirect(url); 。FacesContext.getCurrentInstance()的responseComplete();它給了java.io.UnsupportedEncodingException:只能在ACTION_PHASE –

+0

期間重定向異常說明它實際上是ResourceResponse,請檢查您提出此請求的視圖頁,確保您使用的是正確的請求,而不是 yorkw

1

請嘗試此鏈接,

http://www.portletfaces.org/community/forums/-/message_boards/view_message/76606

和代碼,

在你的JSP或XHTML

<h:commandButton id="btn1" type="submit" action="#{bean.redirectAction}" value="test redirect ajax" /> 
在bean

public void redirectAction() { 
    System.out.println("Action Triggrered"); 
    try { 


     Map<String, Object> reqestMap = FacesContext.getCurrentInstance().getExternalContext().getRequestMap(); 
     ThemeDisplay themeDisplay = (ThemeDisplay) reqestMap.get("THEME_DISPLAY"); 
      FacesContext.getCurrentInstance().getExternalContext().redirect(themeDisplay.getPortalURL() + "/web/{user-id}/home"); 
      } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 

我也面臨這個問題。使用上述解決方案,但沒有工作。 後來發現,這是liferay的portletfaces-bridge jar的問題。 我實際上使用portletfaces-bridge-2.0.0.jar,但在portletfaces-bridge-2.0.1.jar中解決了這個問題。

嘗試使用portletfaces-bridge-2.0.1.jar。和上面的代碼將工作。

如果這不適合你。然後,檢查您是否使用GenericFacesportlet。在這種情況下,你必須使用Liferay 6.0.6。