页面上的多个表单提交到第一个表单?

时间:2014-11-10 03:11:32

标签: forms jsf

我在一个页面中有三个表单,每个表单调用一个不同的支持bean,但是当我使用第一个表单以外的表单时,调用会对第一个表单进行三次调用,每个表单都会持久保存到DB.Table和持久性中当我点击commandButtons时,正在进行三次,这是不好的做法,还是我在代码中遗漏了什么?

    <h:form id="ciclista-form" >            
        <div id="ciclista-link"><h1>Crear Ciclista</h1></div>
             // inputTexts ...  ..  .
        <h:commandButton action="#{ciclistaBean.guardarCiclista()}" value="Guardar Ciclista"  class="ciclista-form-btn"/>   
    </h:form>       
    <h:form id="Etapa-form" >    Etapa-form              -->
        <div id="Etapa-link"><h1>Crear Etapa</h1></div> -->
             //  input texts . ..  
        <h:commandButton action="#{etapaBean.guardarEtapa()}" value="Guardar Etapa"  class="carrera-form-btn"/>  -->
    </h:form>  Etapa-form -->

    <h:form id="carrera-form" >    <!-- carrera-form -->                
        <div id="carrera-link"><h1>Crear carrera</h1></div>
             //   input texts . . . 
        <h:commandButton action="#{carreraBean.guardarCarrera()}" value="Guardar Carrera"  class="etapa-form-btn" />
    </h:form>  <!-- carrera-form -->

所有来电都是第一种形式,为什么会这样?

1 个答案:

答案 0 :(得分:0)

剪了你的节目好吗,你的豆子是什么类型的?他们有什么范围?他们是否依赖于另一个人?

我尝试使用以下代码跟随您的想法,它可以在我的设置中运行...

<h:body>
    <h:form id="ciclista-form">
        <div id="ciclista-link">
            <h1>Crear Ciclista</h1>
            <h:inputText value="#{ciclistaBean.text}"/>
        </div>
        <h:commandButton action="#{ciclistaBean.guardarCiclista()}" value="Guardar Ciclista" class="ciclista-form-btn" />
    </h:form>
    <h:form id="Etapa-form">
        <div id="Etapa-link">
            <h1>Crear Etapa</h1>
            <h:inputText value="#{etapaBean.text}"/>
        </div>
        <h:commandButton action="#{etapaBean.guardarEtapa()}" value="Guardar Etapa" class="carrera-form-btn" />
    </h:form>
    <h:form id="carrera-form">
        <div id="carrera-link">
            <h1>Crear carrera</h1>
            <h:inputText value="#{carreraBean.text}"/>
        </div>
        <h:commandButton action="#{carreraBean.guardarCarrera()}" value="Guardar Carrera" class="etapa-form-btn" />
    </h:form>
</h:body>

有三个独立@RequestScoped beans

@ManagedBean(name = "ciclistaBean")
@RequestScoped
public class SO26836137_F1 {
    private String text;    
    public void guardarCiclista() {
        System.out.println("Form 1 submitted: " + text);
    }   
    public String getText() {
        return text;
    }   
    public void setText(String text) {
        this.text = text;
    }
}


@ManagedBean(name = "etapaBean")
@RequestScoped
public class SO26836137_F2 {
    private String text;
    public void guardarEtapa() {
        System.out.println("Form 2 submitted: " + text);
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
}


@ManagedBean(name = "carreraBean")
@RequestScoped
public class SO26836137_F3 {
    private String text;
    public void guardarCarrera() {
        System.out.println("Form 3 submitted: " + text);
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
}