jsf - 如何防止表单在同一html页面提交时重置其他表单

标签 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);
}
}

在第二个表单上应用操作时,是否有办法防止表单重置?

最佳答案

谢谢@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>

关于jsf - 如何防止表单在同一html页面提交时重置其他表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55658622/

相关文章:

jsf - 在 JSF 数据表中显示 Hibernate/JPA 结果会导致 : java. lang.NumberFormatException:对于输入字符串: "[propertyname]"

jsf - Websphere 上的 javax.enterprise.inject.UnsatisfiedResolutionException

hibernate - javax.validation.validationException : At least one custom message must be created

jsf - 为什么我的 FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal() 代码运行到 NullPointer?

oracle - JSF Faces Bridge 在部署到 oc4j 容器时得到 java.lang.NullPointerException

html - 在页面中间创建内容面板

java - 从 JSF 操作返回 null 和 ""之间的区别

jsf - 不会调用 OmniFaces FullAjaxExceptionHandler 的 logException 方法重写

java - Richfaces 3 数据表排序不起作用

java - 升级到 javaserver faces。轻松的任务?