使用p:remoteCommand为每个ajax请求重新创建ViewScoped bean

时间:2016-03-30 17:29:34

标签: jsf primefaces view-scope remotecommand

我有一个javascript函数负责使用<p:remoteCommand/>调用2个托管bean方法,问题是在一个方法中我必须生成一个值并在第二个方法中使用它,但是当我调用第二个方法时值为null,这是因为每个请求都重新创建了ManagedBean,这是我的代码:

javascriptFile.js

<script>
function executeProcess(){
      method1();
      method2();
}
</script>

myView.xhtml

<h:form>
<p:remoteCommand name="method1"
                        actionListener="#{controller.method1()}"
                        update="xComponent" />
<p:remoteCommand name="method2"
                        actionListener="#{controller.method2()}"
                        update="xComponent" />

</h:form>

Controller.java

@ManagedBean
@ViewScoped
public class SearchCarneStudent implements Serializable {
     private String myValue;

     public void method1(){
           myValue="Hello";
     }

     public void method2(){
         System.out.println(myValue);  //<- This line is returning null because the bean is recreated each request
     }    

}

我希望你能帮助我

由于

0 个答案:

没有答案