如何正确使用f:setPropertyActionListener?

时间:2019-07-09 09:57:02

标签: jsf xhtml

在我的程序中,我想将音乐学生添加到音乐学校的活动中。

有一个.xhtml用于显示数据表中的所有事件。 在数据表中,您可以按一个按钮来更新或删除事件。这两个按钮都将您转到update.xhtml或delete.xhtml。通过使用f:setPropertyActionListener,可以将Event移交给下一个event-bean,它可以正常工作。

update.xhtml也使用f:setPropertyActionListener更新事件,它也可以正常工作。

现在,我想在活动中添加一名音乐系学生的人数(通过一个ArrayList收集活动中的所有音乐系学生),并增加一个页面。

通过按下addStudent-Button从数据表中出来,您将转到添加页面。在那里,您可以在更新页面上看到事件的详细信息,因此f:setPropertyActionListener可以正常工作。当我在输入字段中输入学生的编号并按下添加按钮时,我得到了空指针异常,因为Bean不再知道该事件了。 我在做什么错了?

这是有效的update.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <h:outputStylesheet library="css" name="main.css" />

</h:head>

<h:body>

    <h:form id="eventUpdateForm">
        <h:messages />
        <h3>Update an Event durchfuehren</h3>
        <h:outputLabel value="Event-Nr.: " />
        <h:inputText value="#{eventMB.selectedEventTo.eventNr}"
            readonly="true" />
        <h:inputHidden value="#{eventMB.selectedEventTo.eventNr}" />
        <br></br>

        <h:outputLabel value="Bezeichnung " />
        <h:inputText value="#{eventMB.selectedEventTo.bezeichnung}"></h:inputText>
        <br></br>
        <h:outputLabel value="Ort" />
        <h:inputText value="#{eventMB.selectedEventTo.ort}"></h:inputText>
        <br></br>
        <h:outputLabel value="Anzahl Saxophone" />
        <h:inputText value="#{eventMB.selectedEventTo.anzahlSaxophone}"></h:inputText>
        <br></br>
        <h:outputLabel value="Anzahl Trompeten" />
        <h:inputText value="#{eventMB.selectedEventTo.anzahlTrompeten}"></h:inputText>
        <br></br>
        <h:outputLabel value="Anzahl Posaunen" />
        <h:inputText value="#{eventMB.selectedEventTo.anzahlPosaunen}"></h:inputText>
        <br></br>
        <h:outputLabel value="Anzahl Schlagzeuge" />
        <h:inputText value="#{eventMB.selectedEventTo.anzahlSchlagzeuge}"></h:inputText>
        <br></br>
        <h:outputLabel value="Anzahl Pianos" />
        <h:inputText value="#{eventMB.selectedEventTo.anzahlPianos}"></h:inputText>
        <br></br>
        <h:outputLabel value="Anzahl Bass" />
        <h:inputText value="#{eventMB.selectedEventTo.anzahlBass}"></h:inputText>
        <br></br>
        <h:outputLabel value="Anzahl Gitarren" />
        <h:inputText value="#{eventMB.selectedEventTo.anzahlGitarren}"></h:inputText>
        <br></br>
        <h:outputLabel value="Zeitpunkt (mm.DD.yyyy HH:mm)" />
        <h:inputText value="#{eventMB.selectedEventTo.eventZeitpunkt}"></h:inputText>
        <br></br>

        <br></br>
        <h:commandButton value="Speichern"
            action="#{eventMB.updateEventEnd()}" style=" width: 200px">
            <f:setPropertyActionListener value="#{eventMB.selectedEventTo}"
                target="#{eventMB.selectedEventTo}" />
        </h:commandButton>
    </h:form>

    <h:form>
        <h:commandButton value="Abbruch"
            action="#{eventMB.updateEventAbbrechen()}"
            style=" width: 200px" />
    </h:form>

    <br />


</h:body>
</html>

这是无法正常工作的adding.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <h:outputStylesheet library="css" name="main.css" />
</h:head>

<h:body>

    <h:form id="eventZuordnungForm">
        <h:messages />
        <h3>Schueler zum Event hinzufuegen</h3>
        <h:outputLabel value="Event-Nr.: " />
        <h:inputText value="#{eventMB.selectedEventTo.eventNr}"
            readonly="true" />
        <h:inputHidden value="#{eventMB.selectedEventTo.eventNr}" />
        <br></br>

        <h:outputLabel value="Bezeichnung " />
        <h:inputText value="#{eventMB.selectedEventTo.bezeichnung}" readonly="true"></h:inputText>
        <br></br>
        <h:outputLabel value="Ort" />
        <h:inputText value="#{eventMB.selectedEventTo.ort}" readonly="true"></h:inputText>
        <br></br>
        <h:outputLabel value="Anzahl Saxophone" />
        <h:inputText value="#{eventMB.selectedEventTo.anzahlSaxophone}" readonly="true"></h:inputText>
        <br></br>
        <h:outputLabel value="Anzahl Trompeten" />
        <h:inputText value="#{eventMB.selectedEventTo.anzahlTrompeten}" readonly="true"></h:inputText>
        <br></br>
        <h:outputLabel value="Anzahl Posaunen" />
        <h:inputText value="#{eventMB.selectedEventTo.anzahlPosaunen}" readonly="true"></h:inputText>
        <br></br>
        <h:outputLabel value="Anzahl Schlagzeuge" />
        <h:inputText value="#{eventMB.selectedEventTo.anzahlSchlagzeuge}" readonly="true"></h:inputText>
        <br></br>
        <h:outputLabel value="Anzahl Pianos" />
        <h:inputText value="#{eventMB.selectedEventTo.anzahlPianos}" readonly="true"></h:inputText>
        <br></br>
        <h:outputLabel value="Anzahl Bass" />
        <h:inputText value="#{eventMB.selectedEventTo.anzahlBass}" readonly="true"></h:inputText>
        <br></br>
        <h:outputLabel value="Anzahl Gitarren" />
        <h:inputText value="#{eventMB.selectedEventTo.anzahlGitarren}" readonly="true"></h:inputText>
        <br></br>
        <h:outputLabel value="Zeitpunkt (mm.DD.yyyy HH:mm)" />
        <h:inputText value="#{eventMB.selectedEventTo.eventZeitpunkt}" readonly="true"></h:inputText>
        <br></br>

        <br></br>
        <h:outputLabel value="Schueler-Nr des zuzuordnenden oder zu entfernenden Schuelers angeben" />
        <br></br>
        <h:inputText value="#{eventMB.schuelerNrZuordnung}" />

        //add assigment
        <h:commandButton value="Schueler zuordnen"
            action="#{eventMB.schuelerZuordnungEnd()}" style=" width: 200px">
            <f:setPropertyActionListener value="#{eventMB.selectedEventTo}"
                target="#{eventMB.selectedEventTo}" />
        </h:commandButton>

        //delete assignment
        <h:commandButton value="Schueler vom Event entfernen"
            action="#{eventMB.schuelerZuordnungEntfernenEnd()}" style=" width: 210px">
            <f:setPropertyActionListener value="#{eventMB.selectedEventTo}"
                target="#{eventMB.selectedEventTo}" />
        </h:commandButton>
    </h:form>

    <h:form>
        <h:commandButton value="Abbruch"
            action="#{eventMB.schuelerZuordnungAbbrechenClicked()}"
            style=" width: 200px" />
    </h:form>

    <br />


</h:body>
</html>

我的Event-Bean看起来像这样:

package eventplaner;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import entity.EventTO;
import usecase.IEventPflegen;
import usecase.IEventSuchen;

import java.io.Serializable;

@Named("eventMB")
@RequestScoped
public class EventMB implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -4993586944162555024L;

    @Inject
    IEventPflegen eventPflegenFacade;

    @Inject
    IEventSuchen eventSuchenFacade;

    private String selectedEventNr;
    private EventTO selectedEventTO;
    private String sucheNr;
    private int schuelerNrZuordnung;

    public EventMB() {
    }

    @PostConstruct
    public void initBean() {
        this.selectedEventTO = null;
        this.selectedEventNr = "";
        this.selectedEventTO = new EventTO();
        this.schuelerNrZuordnung = 0;
    }


    public String updateEventStart() {
        System.out.println("Starte Update Event: " + this.selectedEventTO.getEventNr());
        return "UPDATE_EVENT";
    }

........

    public String updateEventEnd() {
        System.out.println("Event speichern: " +this.selectedEventTO.getEventNr());
        eventPflegenFacade.eventAendern(this.selectedEventTO);
        return "BACK_TO_EVENTLISTANZEIGE";
    }

    public String updateEventAbbrechen() {
        return "BACK_TO_EVENTLISTANZEIGE";
    }

    public String deleteEventStart() {

        EventTO EventTO = eventSuchenFacade.eventSuchenMitNummer(this.selectedEventTO.getEventNr());
        System.out.println("Starte das Loeschen von Event: " + EventTO.getEventNr());

        return "DELETE_EVENT";
    }

    public String deleteEventEnd() {
        FacesContext fc = FacesContext.getCurrentInstance();
        Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
        this.selectedEventNr = params.get("selectedEventNr");

        System.out.println("Loesche Event: " + this.selectedEventNr);
        try {
            eventPflegenFacade.eventLoeschen(Integer.valueOf(this.selectedEventNr));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "BACK_TO_EVENTLISTANZEIGE";
    }

    public String deleteEventAbbrechenClicked() {
        System.out.println("Loeschen des Events " + this.getSelectedEventNr() + " abgebrochen!");
        this.initBean();
        return "BACK_TO_EVENTLISTANZEIGE";
    }

    public String starteSchuelerZuordnung() {
        System.out.println("Starte Zuordnung zu Event: " + this.selectedEventTO.getEventNr());
        return "EVENT_SCHUELER_ZUORDNEN";
    }

    public void schuelerZuordnungEnd() {
        this.selectedEventTO.addSchueler(this.schuelerNrZuordnung);
    }

    public void schuelerZuordnungEntfernenEnd() {
        this.selectedEventTO.removeSchueler(this.schuelerNrZuordnung);
    }

    public String schuelerZuordnungAbbrechenClicked() {
        return "BACK_TO_EVENTVW";
    }

    public EventTO getEventTo() {
        return selectedEventTO;
    }

    public void setEventTo(EventTO EventTo) {
        if (this.selectedEventTO == null) {
            this.selectedEventTO = new EventTO();
        }
        this.selectedEventTO = EventTo;
    }

    public EventTO getSelectedEventTo() {
        return selectedEventTO;
    }

    public void setSelectedEventTo(EventTO selectedEventTo) {
        this.selectedEventTO = selectedEventTo;
    }

    public EventTO getSelectedEventTO() {
        return selectedEventTO;
    }

    public void setSelectedEventTO(EventTO selectedEventTO) {
        this.selectedEventTO = selectedEventTO;
    }

    public int getSchuelerNrZuordnung() {
        return schuelerNrZuordnung;
    }

    public void setSchuelerNrZuordnung(int schuelerNrZuordnung) {
        this.schuelerNrZuordnung = schuelerNrZuordnung;
    }

 .......

}

0 个答案:

没有答案