PrimeFaces 如何从 xhtml 更改@ManagedBean 的属性

PrimeFaces how to change attribute of @ManagedBean from xhtml

我想知道是否可以更改属性的值

我试过了,但没用:

<p:menuitem value="ADD" action="#{a.setA2("NewA2Value")}"/>

假设我有这个@ManagedBean class:

@ManagedBean
public class A {
 private String a1;
 private String a2;

 public A() {
 }

 public void setA1(String a1) {
  this.a1 = a1;
 }

 public void setA2(String a2) {
  this.a2 = a2;
 }

 public String getA1() {
  return a1;
 }

 public String getA2() {
  return a2;
 }
}

这里有一些选项。

  1. 方法表达式
  2. f:setPropertyActionListener
  3. f:属性
  4. f:参数

Take a look to this: http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/

示例 f:setPropertyActionListener

<p:menuitem value="ADD" action="#{a.myAction}">
    <f:setPropertyActionListener target="#{a.a1}" value="myValue" />
    <f:setPropertyActionListener target="#{a.a2}" value="myValue2" />
</p:menuitem>

不要忘记 h:form

你的动作应该是这样的。

public String myAction() {
  //String value = this.a1;
  //String value2 = this.a2;
  //You have to return the viewId for the navigation, or null
}

public void myAction() {
  //String value = this.a1;
  //String value2 = this.a2;
}