で 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;
}
}
Web フォーム:
<!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>
望ましくない出力 (「インクリメンタル」ボタンを 5 回クリック):
Valor: 5 Texto: XXXXX TextoBase: A
必要な出力 (「インクリメンタル」ボタンを 5 回クリック):
Valor: 5 Texto: XXXXX TextoBase: AAAAA