2013-08-21 33 views
-1

我有一些會話範圍的bean與f:viewParam一起工作的問題。所以我有兩頁,test_first.xhtmltest_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作爲會話作用域,基本上用於其他屬性。那麼對這個或其他方法有什麼想法?非常感謝!

+1

小小的評論:首先h:鏈接不需要在表單中。 –

+0

相關:http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope/ – BalusC

+0

嗨@BalusC,我更新的問題上的任何想法? – ethanjyx

回答

1

如果您希望它在不同的選項卡或窗口中工作,則需要將這些選項卡特定屬性放在ViewScoped或RequestScoped Bean上。對於會話特定的屬性,您可以創建另一個Bean並將其設置爲SessionScoped。

相關問題