2

I have two different forms and if i apply action on one form the fields values in the second form is resetting(cleared or backed to default values ), i have not know idea what's going on ?

i am using Primefaces 7 with JSF (jsf-api 2.2.11, jsf-impl 2.2.11 and javax.servlet-api 3.0.1).

i have tried to override Sammy function from below post

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

also i tried to save cache

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

and here is my first form code

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

and the second 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>

i update the post to set Bean implementation below

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

Is there is away to prevent form reset when applying actions on the second form?

1 Answers1

2

thanks @Kukeltje it's all about ajax

just update specific form that you want to update it

<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>
Wai Ha Lee
  • 7,664
  • 52
  • 54
  • 80