我有一些會話範圍的bean與f:viewParam
一起工作的問題。所以我有兩頁,test_first.xhtml
和test_second.xhtml
,以及TestBean.java
。在會話範圍的bean中查看參數
first.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head/>
<h:body>
<h:form>
<h:link value="link1" outcome="test_second" >
<f:param name="id" value="1"/>
</h:link>
<br/><br/>
<h:link value="link2" outcome="test_second" >
<f:param name="id" value="2"/>
</h:link>
</h:form>
</h:body>
</html>
second.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<f:metadata>
<f:viewParam name="id" value="#{testBean.userId}" />
</f:metadata>
<h:head/>
<h:body>
<h:form>
This is the second page.
<h:outputText value="Selected id is #{testBean.userId}" />
<h:commandButton value="Print page id" action="#{testBean.print()}" />
<h:commandButton styleClass="submitButton" value="Submit" action="#{testBean.submit}">
<f:ajax execute="@form" render="@form"/>
</h:commandButton>
</h:form>
</h:body>
</html>
TestBean.java:
@ManagedBean
@SessionScoped
public class TestBean implements Serializable{
private Integer userId;
public void print() {
System.out.println(userId);
}
public void submit() {
System.out.println(userId);
}
/...
}
開始從first.xhtml運行,如果我在打開link1
新標籤,然後在另一個新選項卡中打開link2
。現在我有兩頁。
如果我在鏈接1中單擊「打印頁面ID」按鈕,將在控制檯中打印1
。在link2中,打印值將是2
。
但如果我點擊鏈接1的Submit
按鈕,2
將會打印出來,呈現的文本將從1更改爲2(因爲鏈接2稍後打開,豆是會話範圍?)
(更新:爲什麼會出現這種情況?如果點擊「提交」,我仍然可以打印「1」)
我想保留bean作爲會話作用域,基本上用於其他屬性。那麼對這個或其他方法有什麼想法?非常感謝!
小小的評論:首先h:鏈接不需要在表單中。 –
相關:http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope/ – BalusC
嗨@BalusC,我更新的問題上的任何想法? – ethanjyx