-1

で動作するセッション スコープ Bean に問題がありf:viewParamます。したがって、test_first.xhtmltest_second.xhtml、 、 の2 つのページがありTestBean.javaます。

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);
}
    /...
}

link1新しいタブで開いてから別の新しいタブで開くと、first.xhtml から実行を開始しlink2ます。これで2ページになりました。

link1 の [Print page id] ボタンをクリックする1と、コンソールに出力されます。link2 では、出力される値は になります2

しかし、link1 のSubmitボタンをクリックする2と、出力され、レンダリングされたテキストが 1 から 2 に変わります。

(更新:このケースはなぜですか?「送信」をクリックしても「1」を出力するにはどうすればよいですか?)

基本的に、Bean をセッション スコープの他のプロパティとして保持したいと考えています。それで、これまたは代替方法について何か考えはありますか?どうもありがとう!

4

1 に答える 1

1

別のタブまたはウィンドウで動作させたい場合は、それらのタブ固有のプロパティを ViewScoped または RequestScoped Bean に配置する必要があります。セッション固有のプロパティの場合、別の Bean を作成してそれを SessionScoped にすることができます。

于 2013-08-21T05:51:56.627 に答える