我正在使用JSF 2.1,我有一个h:selectOneMenu
组件,所选值(例如:3)
将显示3个h:inputText
组件。选择值后,我的h:commandButton
提交null
值到支持bean,我得到异常。我使用一个支持bean,范围是会话。
我尝试了两种解决方案:
h:form
个组件,第一个用于h:selectOneMenu
,第二个用于h:inputText
。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];
}
}
答案 0 :(得分: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 Blog : Learning JSF2: Ajax in JSF – using f:ajax tag