如何防止表单在同一HTML页面中提交时重置其他表单

时间:2019-04-12 19:55:43

标签: jsf

我有两种不同的形式,如果我对一种形式应用操作,则第二种形式中的字段值将被重置(清除或恢复为默认值),我不知道发生了什么?

我正在将Primefaces 7与JSF(jsf-api 2.2.11,jsf-impl 2.2.11和javax.servlet-api 3.0.1)结合使用。

我试图从下面的帖子中覆盖Sammy函数

https://stackoverflow.com/questions/10652881/how-to-prevent-form-submit#I%20also%20had%20this%20problem%20and%20I%20found%20a%20solution%20in

我也试图保存缓存

< -meta http-equiv="cache-control" content="cache, store" / ->

这是我的第一个表单代码

    <h:form id="createStageForm" class="form-horizontal">
            <h:outputLabel >Notes :</h:outputLabel>
                <h:inputText value="#{stageBean.stageDto.notes}"/>
                    <h:commandButton value="Save" action="#{stageBean.saveDetails}"/>
    </h:form>

和第二种形式

        <h:form>
            <h:dataTable value="#{stageBean.items}" var="item">
                <h:column>
                    <h:inputText value="#{item.name}" />
                </h:column>
                <h:column>
                    <h:commandButton value="-" action="#{stageBean.remove(item)}" />
                </h:column>
            </h:dataTable>
            <h:commandButton value="+" action="#{stageBean.add}" />
        </h:form>

我更新了帖子以在下面设置Bean实现

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class StageBean {

private List<EmployeeDto> items;
public ArrayList<StageDto> listFromDB;
private StageDto stageDto;

@PostConstruct
public void init() {
    listFromDB = SatgeDatabaseOperation.getListFromDB();
    items = new ArrayList<EmployeeDto>();
    items.add(new EmployeeDto());
}

private long id;
private String notes;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}
public String getNotes() {
    return notes;
}

public void setNotes(String notes) {
    this.notes = notes;
}

public void add() {
    items.add(new EmployeeDto());
}

public void remove(EmployeeDto item) {
    items.remove(item);
}

public void save() {
    System.out.println("items: " + items);
}

public ArrayList<StageDto> getListFromDB() {
    return listFromDB;
}

public void setListFromDB(ArrayList<StageDto> listFromDB) {
    this.listFromDB = listFromDB;
}

public String saveDetails() {
    return SatgeDatabaseOperation.saveDetailsInDB(stageDto);
}
}

在第二种表单上执行操作时,是否有防止表单重置的方法?

1 个答案:

答案 0 :(得分:0)

感谢@Kukeltje这完全是关于ajax

仅更新要更新的特定表单

<h:form id="myForm2">
    <h:dataTable value="#{stageBean.items}" var="item">
        <h:column>
            <h:inputText value="#{item.name}" />
        </h:column>
        <h:column>
            <h:commandButton value="-" action="#{stageBean.remove(item)}" />
        </h:column>
    </h:dataTable>
    <h:commandButton value="+" action="#{stageBean.add}" >
        <f:ajax execute="@form" render="myForm2" />
    </h:commandButton>
</h:form>