1

私は JSF 2.1 を使用しています。h:selectOneMenuコンポーネントがあり、選択した値 (例:3) には 3 つのh:inputTextコンポーネントが表示されます。値を選択すると、バッキング Bean に値がh:commandButton送信され、例外が発生します。null1 つのバッキング Bean を使用します。スコープはセッションです。

私は2つの解決策を試しました:

  • 2 つh:formのコンポーネントを使用します。1 つ目は s 用でh:selectOneMenu、2つ目はh:inputTexts 用です。
  • renderedから選択した値に従って属性をいじりますh:selectOneMenu

Bean では、両方ではなく、h:selectOneMenuまたは の値から選択した値を取得しましたh:inputText

<h:form id="f1">
    Nombre d'opérandes :
    <h:selectOneMenu value="#{c.n}" onchange="submit()"    >
        <f:selectItem id="item1" itemLabel="1" itemValue="1" />
        <f:selectItem id="item2" itemLabel="2" itemValue="2" />
        <f:selectItem id="item3" itemLabel="3" itemValue="3" />
        <f:selectItem id="item4" itemLabel="4" itemValue="4" />
        <f:selectItem id="item5" itemLabel="5" itemValue="5" />
    </h:selectOneMenu>
    <h:panelGrid rendered="#{c.n!=0}">
        <c:forEach var="x" begin="0" end="#{c.n-1}"  >
            <h:outputLabel   value="Opérande #{x+1} : "/>      
            <h:inputText  value="#{c.op2[x]}"  />     <br/>
        </c:forEach>
        <fieldset border="1" style="margin:0 auto;width:150px;text-align: center;border-radius:5px;">
            <legend>Operations </legend>
            <h:selectOneRadio value="#{c.operation}" style="margin:0 auto;">
                <f:selectItem itemValue="+" itemLabel="+"/>
                <f:selectItem itemValue="-" itemLabel="-"/>
                <f:selectItem itemValue="x" itemLabel="x"/>
            </h:selectOneRadio>
        </fieldset>
        <h:commandButton action="#{c.Calcul}" value="Valider"  style="background-color:blue;color:white;" />
        <br/>
        <h:outputLabel value="Résultat : #{c.res}"/>
    </h:panelGrid>
</h:form>  

バッキング Bean:

@ManagedBean(name="c")
@SessionScoped
public class Calcul {

    Double[] op2;
    private int n;
    private double res;
    private String operation;
    public Calcul(){

        op2=new Double[100];

    }

    public double getRes() {
        return res;
    }

    public void setRes(double res) {
        this.res = res;
    }


    public Double[] getOp2() {

        return op2;
    }

    public void setOp2(Double[] op2) {
        this.op2 = op2;
    }

     public int getN() {
        return n;
    }

    public void setN(int n) {
        this.n = n;
    }

    public String getOperation() {

        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public void Calcul(){
        if ( operation.equals("+")) { 
            somme();      
        }
        else if ( operation.equals("-"))   soustraction();
        else if ( operation.equals("x")) multiplication();
    }

    public void somme() {
        res=0;
        for (int i = 0; i < n; i++) res+=op2[i];
    }
    public void soustraction() {
        res=op2[0];
        for (int i = 1; i < n; i++) res-=op2[i];
    }
    public void multiplication() {
        res=1;
        for (int i = 0; i < n; i++) res*=op2[i];
    }
}
4

1 に答える 1

1

使用するf:ajax

交換

 <h:selectOneMenu value="#{c.n}" onchange="submit()"    >
    <f:selectItem id="item1" itemLabel="1" itemValue="1" />
    <f:selectItem id="item2" itemLabel="2" itemValue="2" />
    <f:selectItem id="item3" itemLabel="3" itemValue="3" />
    <f:selectItem id="item4" itemLabel="4" itemValue="4" />
    <f:selectItem id="item5" itemLabel="5" itemValue="5" />
</h:selectOneMenu>

<h:selectOneMenu value="#{c.n}">
    <f:selectItem id="item1" itemLabel="1" itemValue="1" />
    <f:selectItem id="item2" itemLabel="2" itemValue="2" />
    <f:selectItem id="item3" itemLabel="3" itemValue="3" />
    <f:selectItem id="item4" itemLabel="4" itemValue="4" />
    <f:selectItem id="item5" itemLabel="5" itemValue="5" />
    <f:ajax render="@form"/>
</h:selectOneMenu>

そして交換

<h:commandButton action="#{c.Calcul}" value="Valider"  style="background-color:blue;color:white;" />

<h:commandButton action="#{c.Calcul}" value="Valider"  style="background-color:blue;color:white;" >
    <f:ajax execute="@form" render="@form"/>
</h:commandButton>

最も重要な

このブログを読むMaxa ブログ : Learning JSF2: Ajax in JSF – using f:ajax タグ

于 2012-12-03T06:56:40.220 に答える