我在构建 JSF 项目时遇到了问题ViewScope
ManagedBeans
。所以我决定将问题隔离为一个简单的代码,显然问题仍然存在。基本上我意识到类中的属性可以TesteMBean
完美地工作,但是,从超类继承的属性GenericoMBean
不能保留它的值。
我还是初学者,所以不知道这是否是 JSF 的正常行为。有人可以启发我吗?如果可能的话,通过一个例子告诉我如何正确地做到这一点。非常感谢您。
按照代码。
超类:
package br.com.telesnet.sige.web.mb;
public abstract class GenericoTesteMBean {
protected String textoBase;
public java.lang.String getTextoBase() {
if (this.textoBase == null){
return "";
}else{
return textoBase;
}
}
public void setTextoBase(String textoBase) {
this.textoBase = textoBase;
}
}
继承的 ManagedBean:
package br.com.telesnet.sige.web.testes;
import java.io.Serializable;
import javax.annotation.ManagedBean;
import javax.faces.bean.ViewScoped;
import br.com.telesnet.sige.web.mb.GenericoTesteMBean;
@ManagedBean
@ViewScoped
public class TesteMBean extends GenericoTesteMBean implements Serializable{
private static final long serialVersionUID = 1L;
private java.lang.Integer valor;
private java.lang.String texto;
public TesteMBean(){
this.setValor(0);
}
public void incrementar(){
this.setValor(this.getValor()+1);
this.texto = this.getTexto() + "X";
this.textoBase = this.getTextoBase() + "A";
}
public java.lang.Integer getValor() {
if (this.valor == null){
return 0;
}else{
return valor;
}
}
public void setValor(java.lang.Integer valor) {
this.valor = valor;
}
public java.lang.String getTexto() {
if (this.texto == null){
return "";
}else{
return texto;
}
}
public void setTexto(java.lang.String texto) {
this.texto = texto;
}
}
网络表格:
<!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://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:body>
<f:view>
<h:form id="formLabel">
<h:outputLabel value="Valor:"></h:outputLabel>
<h:outputLabel value="#{testeMBean.valor}"></h:outputLabel>
<h:outputLabel value="Texto:"></h:outputLabel>
<h:outputLabel value="#{testeMBean.texto}"></h:outputLabel>
<h:outputLabel value="TextoBase:"></h:outputLabel>
<h:outputLabel value="#{testeMBean.textoBase}"></h:outputLabel>
</h:form>
<h:form id="formIncremento">
<h:commandButton actionListener="#{testeMBean.incrementar()}" value="incrementar">
</h:commandButton>
</h:form>
</f:view>
</h:body>
</html>
不想要的输出(按钮“incrementar”点击了五次):
Valor: 5 Texto: XXXXX TextoBase: A
所需的输出(按钮“incrementar”点击五次):
Valor: 5 Texto: XXXXX TextoBase: AAAAA