Hi there,
I'm starting to code a little cookbook. Therefor i've created a page to add some menusteps, a menustep should describe how to cook the menu step by step. I have a datatable containing a list of MenuSteps. The user can click a commandLink to add/delete a new step. The Bean adds or removes a MenuStep of the list and rerender the datatable with ajax. That works great, except I lost all the data given to the two inputText fields for the steps. They are all blank after I add or delete a row of my datatable.
I hope someone can help me. Below you can see my code. Let me know if you need more input!
Best regards, Benjamin
Here is my XHTML (it's a part of a template):
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:util="http://java.sun.com/jsf/composite/components/util"
xmlns:f="http://java.sun.com/jsf/core">
<h:form id="form">
<h:dataTable id="menuStepTable" styleClass="longTable" value="#{MenuCreation.menuSteps}" var="step">
<h:column>
<h:panelGrid columns="2" styleClass="longTable" columnClasses="profileColumn1,profileColumn2">
<h:outputLabel for="inputStepName" value="Name:"/>
<h:inputText id="inputStepName" value="#{step.stepName}"/>
<h:outputLabel for="inputTask" value="Beschreibung:"/>
<h:inputText id="inputTask" value="#{step.task}"/>
<h:commandLink styleClass="safe" value="Schritt entfernen" action="#{MenuCreation.removeMenuStepRow(step)}">
<f:ajax execute="@this" render="@form"/>
</h:commandLink>
</h:panelGrid>
<hr />
</h:column>
</h:dataTable>
<h:commandLink styleClass="safe" value="Schritt hinzufügen" action="#{MenuCreation.addMenuStepRow}">
<f:ajax execute="@this" render="@form"/>
</h:commandLink>
</h:form>
</ui:composition>
Here is my ManagedBean:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package de.charite.ne.server.admin.menue;
import de.charite.ne.server.persistence.menu.MenuStep;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
/**
*
* @author benjamin
*/
@ManagedBean(name = "MenuCreation")
@SessionScoped
public class MenuCreationBean implements Serializable{
private List<MenuStep> menuSteps = new ArrayList<MenuStep>();
@PostConstruct
public void init() {
MenuStep menuStep = new MenuStep();
menuStep.setStepName("Neuer Schritt...");
menuStep.setTask("Beschreibung des Schrittes...");
menuSteps.add(menuStep);
}
public void addMenuStepRow() {
MenuStep menuStep = new MenuStep();
menuSteps.add(menuStep);
}
public void removeMenuStepRow(MenuStep menuStep) {
menuSteps.remove(menuStep);
}
public List<MenuStep> getMenuSteps() {
return menuSteps;
}
public void setMenuSteps(List<MenuStep> menuSteps) {
this.menuSteps = menuSteps;
}
}
Here is my Entity:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package de.charite.ne.server.persistence.menu;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author benjamin
*/
@Entity
@Table(name = "menustep")
public class MenuStep implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private int sequenceNumber;
private String stepName;
private String task;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getSequenceNumber() {
return sequenceNumber;
}
public void setSequenceNumber(int sequenceNumber) {
this.sequenceNumber = sequenceNumber;
}
public String getStepName() {
return stepName;
}
public void setStepName(String stepName) {
this.stepName = stepName;
}
public String getTask() {
return task;
}
public void setTask(String task) {
this.task = task;
}
}