Bean 中的变量不通过按钮点击保持其价值
Variable in Bean is not holding its value through Button Clicks
这是我的第一个问题。
我试图在使用 primefaces 单击按钮时增加文本框中的变量值。
但是通过调试我发现每当我单击 p:commandButton 时,EL bean 函数调用都会尝试增加 bean class 变量 "counter",每当这种情况发生时计数器始终为 0,因此它会增加到 1,这显示在我的网页中。它永远不会达到 2、3、4...
代码如下:
xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:form>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel value="Counter:"/>
<h:outputText id="output" value="#{counterView.counter}" />
<p:commandButton value="Count" action="#{counterView.increment}" update="output" />
</h:panelGrid>
</h:form>
</h:body>
</html>
Java:
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;
@ManagedBean(name="counterView")
@ViewScoped
public class counterView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2369382392318418242L;
private int counter = 0;
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
public void increment() {
// Counter is 0 always the moment this function is entered...
this.counter = this.counter + 1;
}
}
我无法弄清楚我哪里出错了,因为它是 primefaces 展示中的一个 class 典型示例,我正在关注它...
提前致谢
您使用了错误的 ViewScoped 注释。您必须将 javax.faces.bean.* 注释与 @ManagedBean.
结合使用
参见:@SessionScoped bean looses scope and gets recreated all the time, fields become null
这是我的第一个问题。
我试图在使用 primefaces 单击按钮时增加文本框中的变量值。
但是通过调试我发现每当我单击 p:commandButton 时,EL bean 函数调用都会尝试增加 bean class 变量 "counter",每当这种情况发生时计数器始终为 0,因此它会增加到 1,这显示在我的网页中。它永远不会达到 2、3、4...
代码如下:
xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:form>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel value="Counter:"/>
<h:outputText id="output" value="#{counterView.counter}" />
<p:commandButton value="Count" action="#{counterView.increment}" update="output" />
</h:panelGrid>
</h:form>
</h:body>
</html>
Java:
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;
@ManagedBean(name="counterView")
@ViewScoped
public class counterView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2369382392318418242L;
private int counter = 0;
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
public void increment() {
// Counter is 0 always the moment this function is entered...
this.counter = this.counter + 1;
}
}
我无法弄清楚我哪里出错了,因为它是 primefaces 展示中的一个 class 典型示例,我正在关注它...
提前致谢
您使用了错误的 ViewScoped 注释。您必须将 javax.faces.bean.* 注释与 @ManagedBean.
结合使用参见:@SessionScoped bean looses scope and gets recreated all the time, fields become null