编辑可编辑数据表时丢失数据(JSF2)

时间:2022-09-17 11:38:17

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.

我开始编写一本小食谱。因此我创建了一个页面来添加一些菜单步骤,菜单步骤应该描述如何逐步烹饪菜单。我有一个包含MenuSteps列表的数据表。用户可以单击commandLink来添加/删除新步骤。 Bean添加或删除列表的MenuStep并使用ajax重新呈现数据表。这很好用,除了我丢失了给步骤的两个inputText字段的所有数据。添加或删除一行数据表后,它们都是空白的。

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

这是我的XHTML(它是模板的一部分):

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

这是我的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;
    }
}

1 个解决方案

#1


0  

I solved it.

我解决了

<h:commandButton styleClass="safe" value="Zutat hinzufügen" action="#{MenuCreation.addInTakeRow}">
    <f:ajax disabled="true" execute="@this" render="@form"/>
</h:commandButton>

disabled=true for each of those ajax commands.

对于每个ajax命令,disabled = true。

#1


0  

I solved it.

我解决了

<h:commandButton styleClass="safe" value="Zutat hinzufügen" action="#{MenuCreation.addInTakeRow}">
    <f:ajax disabled="true" execute="@this" render="@form"/>
</h:commandButton>

disabled=true for each of those ajax commands.

对于每个ajax命令,disabled = true。