2012-09-10 44 views
2

我有一個應用程序使用PrettyFaces和PrimeFaces。使用URL模式/#{ location : navigationBean.location }/#{ year : navigationBean.year }訪問一個頁面。PrettyFaces導航selectOneMenu更改

在該頁面上,我有一個p:selectOneMenu包含可能的位置值。如果我更改此菜單,我希望頁面能夠在同一年和新位置進行刷新。

我已經測試了這個使用AJAX來設置導航Bean中的位置,然後按結果pretty:viewview是url-mapping id)按p:commandButton。這工作,但我想擺脫按鈕。

我已經嘗試了以下,但可悲的是方法中的返回String不執行導航。

的JSF:

<p:selectOneMenu value="#{navigationBean.location}"> 
    <p:ajax listener="#{navigationBean.navigate}"/> 
    <f:selectItems value="#{locationBean.locations}"/> 
</p:selectOneMenu> 

支持bean:

public String navigate(AjaxBehaviorEvent event) 
{ 
    return (location != null) ? "pretty:view" : null; 
} 

的PrettyFaces配置:

<url-mapping id="view"> 
    <pattern value="/#{ location : navigationBean.location }/#{ /\\d{4}/ year : navigationBean.year }/"/> 
    <view-id value="/view.xhtml"/> 
</url-mapping> 

回答

2

您不能返回從(AJAX)動作監聽器導航的結果。您只能從<h:commandButton action>中使用的實際操作方法中返回。

改爲使用NavigationHandler#handleNavigation()

public void navigate() { 
    FacesContext context = FacesContext.getCurrentInstance(); 
    context.getApplication().getNavigationHandler() 
     .handleNavigation(context, null, (location != null) ? "pretty:view" : null); 
} 
+0

非常感謝!對於記錄'ExternalContext#redirect()'不適用於PrettyFaces。笨拙的代碼對我來說是一個錯誤(錯過了簡化示例的地方,更新了我的問題)。 – siebz0r

+1

不客氣。 'ExternalContext#redirect()'只需要**真實**網址,而不是JSF視圖ID或導航結果,更不用說PrettyFaces視圖ID或導航結果。 – BalusC

+0

解釋了很多。爲了理智的原因,我編輯了您的答案(使其與最新的答案匹配)。再次感謝。 – siebz0r