两个提交动作在一个h:表单中

时间:2012-12-02 18:48:58

标签: forms jsf

我正在使用JSF 2.1,我有一个h:selectOneMenu组件,所选值(例如:3) 将显示3个h:inputText组件。选择值后,我的h:commandButton提交null值到支持bean,我得到异常。我使用一个支持bean,范围是会话。

我尝试了两种解决方案:

  • 使用2 h:form个组件,第一个用于h:selectOneMenu,第二个用于h:inputText
  • 根据从rendered
  • 中选择的值播放h:selectOneMenu属性

在bean中,我从h:selectOneMenuh: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];
    }
}

1 个答案:

答案 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