2012-11-20 50 views
0

我是JSF的新手。我的應用程序正在運行,但在使用控制器時,我對瀏覽器中的鏈接感到困惑。順便說一句,也有PrimeFaces在我的應用程序,所以不要驚訝與p:標籤。假設我有'list'和'show'頁面,控制器在它們之間進行重定向。首先我在http://localhost:8080/y/r/conversation/list.xhtml頁面。有鏈接創建與行<p:commandLink action="#{lazyConversationBean.doShow(conv)}" ajax="false" value="View"/>。 lazyConversationBean在這裏充當我的Controller。有方法:jsf搞砸了鏈接

public String doShow(Conversation c) { 
    this.setSelectedConversation(c); 
    return "view"; 
} 

從中我得到了重定向到......再次http://localhost:8080/y/r/conversation/list.xhtml(瀏覽器顯示它),即使它是正確的http://localhost:8080/y/r/conversation/view.xhtml頁。在那裏,我有鏈接<p:commandButton action="#{lazyConversationBean.doList()}" ajax="false" value="Back to list"/>又一次控制器具有方法:

public String doList() { 
    return "list"; 
} 

從中我得到重定向到...是的,你猜對了... http://localhost:8080/y/r/conversation/view.xhtml(即又是什麼瀏覽器顯示),即使這又是正確的http://localhost:8080/y/r/conversation/list.xhtml頁面。

它作爲瀏覽器鏈接區域的接縫總是比當前正在顯示的頁面落後一步。我甚至不知道這是不是一個不正確的行爲,因爲我不知道如何查詢谷歌這個:D只是爲了測試我做了this簡短的教程,其中netbeans創建了一堆我的實體上的代碼,行爲是相同,所以它不是PrimeFaces魔術相關的。

你能告訴我爲什麼會發生,以及如何解決它?用戶喜歡複製正確的鏈接;)

回答

2

這不是問題,框架是這樣做的。有不同的方法來解決這個問題。例如,您可以添加創建類似的動作:

public String doShow(Conversation c) 
{ 
    this.setSelectedConversation(c); 
    return "view" + "?faces-redirect=true"; 
} 

<p:commandLink action="#{lazyConversationBean.doShow(conv)}" ajax="false" value="View"/> 

它會強制重定向到操作視圖,以便瀏覽器中的URL發生更改。

編輯:這裏是faces-config.xml中的導航解決方案(注意<redirect />):

<navigation-rule> 
    <navigation-case> 
     <from-outcome>outcome1</from-outcome> 
     <to-view-id>/outcome1.xhtml</to-view-id> 
     <redirect /> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>outcome2</from-outcome> 
     <to-view-id>/outcome2.xhtml</to-view-id> 
     <redirect /> 
    </navigation-case> 
</navigation-rule> 
+0

我不能做到這一點。部署異常顯示:'javax.el.E​​LException:不是有效的方法表達式:#{lazyConversationBean.doShow(conv)}?faces-redirect = true' – Mateusz

+0

和內部括號其運行時異常:'javax.el.E​​LException:Error Parsing :#{lazyConversationBean.doShow(conv)?faces-redirect = true}' – Mateusz

+0

哦,對不起,等一下我會編輯 –