2

で 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
4

3 に答える 3

4

3 つのファイルをワークスペースにコピーするだけで、「インクリメンタル」が完全に機能します。

唯一の違いはマネージド Bean にあります。@ManagedBean アノテーションにはこのインポートがあります

import javax.annotation.ManagedBean;

に変更します

import javax.faces.bean.ManagedBean;

Java EE 6 @javax.annotation.ManagedBean 対 @javax.inject.Named 対 @javax.faces.ManagedBeanの概念についてこれを読むことができます

于 2013-03-19T18:04:36.393 に答える
1

更新
あなたの問題を見ているので、私の古い答えを削除しました。

  1. h:formあなたの周りにa は必要ありませんh:outputLabelh:outputPanelそれらの周りに何かを使用するだけで十分です。
  2. あなたは anの代わりにactionListenerin yourを使用しています- その場合は問題ありませんが、違いを知っていますか? さらに、属性内の括弧を削除できます。h:commandButtonactionactionListener
  3. その後incrementar、問題を見つけるのに役立ついくつかのデバッグ メッセージを使用してみてください。
  4. ajax 機能を使用している場合はh:outputLabel、新しく作成した 内のを更新する必要がありますh:outputPanel

これだけ:

<h:outputPanel id="myOutputPanel">
   ... your h:outputLabels here ...
</h:outputPanel>

<h:form id="formIncremento">
  <h:commandButton action="#{testeMBean.incrementar}" update=":myOutputPanel">
</h:form>

ただし、提供されているコードは ajax 機能を使用していません。つまり、変更を表示するには、ページをリロードする必要があります (ブラウザーで更新またはF5 キーを押す)。@ViewScopedBean はページの更新後に破棄され、その直後に作成されるため、Bean ではうまく機能しません。を使用するか、primefaces のような機能が組み込まれたフレームワークを使用してみて<f:ajax...>ください。h:commandButtonp:commandButton

于 2013-03-19T16:48:34.617 に答える
0

プロパティがリクエストで渡されるようにするには、抽象基本クラスで Serializable を実装する必要があります。

于 2014-06-06T14:06:07.410 に答える