私は JSF 2.1 を使用しています。h:selectOneMenu
コンポーネントがあり、選択した値 (例:3) には 3 つのh:inputText
コンポーネントが表示されます。値を選択すると、バッキング Bean に値がh:commandButton
送信され、例外が発生します。null
1 つのバッキング Bean を使用します。スコープはセッションです。
私は2つの解決策を試しました:
- 2 つ
h:form
のコンポーネントを使用します。1 つ目は s 用でh:selectOneMenu
、2つ目はh:inputText
s 用です。 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];
}
}