jsf 按钮在第二次单击后做出反应



我的jsf页面遇到了很大的问题。我必须将 Post 请求中的参数传递给请求范围的 Bean。除了一个小错误外,一切正常。更新我信息的按钮仅在第二次单击时做出反应。此页面必须是请求范围。每次我按下按钮时,它首先运行@PostConstruct,然后准备好单击。我试图删除PostConstruct方法,但我的程序停止了。我几乎尝试了所有方法。有什么想法吗?

这是我的代码:我通过从另一个Bean调用Methode"showEditEvent"来进入此页面。

package model.backingBean.event;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ResourceBundle;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
//import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.Part;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import model.backingBean.system.SessionBean;
import model.bean.event.Event;
import model.database.EventManager;
import model.enums.EColorTheme;
import model.exceptions.DatabaseException;
import model.utility.Cryptography;
import model.utility.system.ConfigurationReader;
/**
 * Bean for is responsible for the edit actions, which are performed with an
 * event. That means you can change eventdetails.
 * 
 * @author 101 Computing
 * 
 */
@ManagedBean
@RequestScoped
public class EditEventBean implements Serializable {
private static final long serialVersionUID = 1L;
private Event event;
private UIComponent generateVerCode;
private UIComponent updateButton;
private Part uploadedImage;
private static Logger logger = LogManager.getLogger(EditEventBean.class
        .getName());
private String color;
private ResourceBundle rb = FacesContext.getCurrentInstance().getApplication().
        getResourceBundle(FacesContext.getCurrentInstance(),"msgs");
@ManagedProperty("#{param.eventID}")
private int eventID;
/**
 * Injects SessionBean.
 */
@ManagedProperty("#{sessionBean}")
private SessionBean sb;
public String showEditEvent(int eventID) {
    FacesContext fcxt = FacesContext.getCurrentInstance();
    this.setEventID(eventID);
    try {
        this.event = EventManager.getEvent(eventID);
    } catch (DatabaseException e) {
        FacesMessage msgs = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                rb.getString("exceptions_database"), null);
        fcxt.addMessage(null, msgs);
    }
    return "/facelets/eventAdmin/editEvent.xhtml";
}
public int getEventID() {
    return eventID;
}
public void setEventID(int eventID) {
    this.eventID = eventID;
}
/**
 * Method initializes the event.
 * 
 * @throws IOException
 */
@PostConstruct
public void init() {
    FacesContext fcxt = FacesContext.getCurrentInstance();

    String id = FacesContext.getCurrentInstance().getExternalContext()
            .getRequestParameterMap().get("eventID");
    if (id != null) {
        this.eventID = Integer.parseInt(id);
        try {
            this.event = EventManager.getEvent(this.eventID);
        } catch (DatabaseException e) {
            FacesMessage msgs = new FacesMessage(
                    FacesMessage.SEVERITY_ERROR,
                    rb.getString("exception_database"), null);
            fcxt.addMessage(null, msgs);
        }
    }
}
/**
 * 
 * @param eventID
 * @return
 */
public void update(int eventID) {
    FacesContext fcxt = FacesContext.getCurrentInstance();

    if (this.event.getEndOfEvent().after(this.event.getStartOfEvent())
            && this.event.getEntireTicketamount() > 0) {
        if (this.uploadedImage != null) {
            upload();
        }
        if (this.color.equals(EColorTheme.THEME_PINK.toString())) {
            this.event.setColorTheme(EColorTheme.THEME_PINK);
        } else if (this.color.equals(EColorTheme.THEME_BLUE.toString())) {
            this.event.setColorTheme(EColorTheme.THEME_BLUE);
        }
        EventManager.updateEvent(this.event);
        try {
            this.event = EventManager.getEvent(this.eventID);
        } catch (DatabaseException e) {
            FacesMessage msgs = new FacesMessage(
                    FacesMessage.SEVERITY_ERROR,
                    rb.getString("exception_database"), null);
            fcxt.addMessage(null, msgs);
        }
        // Message for updating
        FacesMessage msgs = new FacesMessage(FacesMessage.SEVERITY_INFO,
                rb.getString("editEvent_successUpdate"), null);
        fcxt.addMessage(null, msgs);
    } else {
        // Message for updating
        FacesMessage message = new FacesMessage("Not updated "
                + "- ends before it starts!");
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(updateButton.getClientId(context), message);
    }
}
/**
 * Method generates a new verification code and saves it in the database.
 */
public void generateNewVerificationCode(int eventID) {
    FacesContext fcxt = FacesContext.getCurrentInstance();
    String newCode = verificationGenerator();
    this.event.setVerificationCode(newCode);
    try {
        EventManager.setNewVerificationCode(this.event.getEventID(),
                newCode);
    } catch (DatabaseException e) {
        FacesMessage msgs = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                rb.getString("exception_database"), null);
        fcxt.addMessage(null, msgs);
    }
    FacesMessage message = new FacesMessage("New VC generated.");
    FacesContext context = FacesContext.getCurrentInstance();
    context.addMessage(generateVerCode.getClientId(context), message);
    logger.info("Generated a new verification code for event with id="
            + this.event.getEventID() + ".");
}

这是我的面孔

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<ui:composition template="../../templates/basicTemplate.xhtml">
    <ui:define name="metadata">
        <f:metadata>
            <f:viewParam name="eventID" value="#{editEventBean.eventID}" />
            <f:viewAction action="#{editEventBean.init}" />
        </f:metadata>
    </ui:define>
    <ui:define name="content">
        <h1 style="color: #808080; font-size: large">#{msgs.event_editEvent_h1}
        </h1>
        <br />
        <h:form enctype="multipart/form-data">
            <f:view>
                <input type="hidden" name="eventID"
                    value="#{editEventBean.eventID}" />
                <h:outputText value="#{msgs.editEvent_pictureSize}" /> <br/>
                <h:panelGrid columns="3">
                    <h:outputLabel for="eventPic" value="#{msgs.event_picture}" />
                    <h:inputFile id="eventPic" value="#{editEventBean.uploadedImage}" sizeLimit="20480"/>
                    <h:message for="eventPic" errorStyle="color:red" />
                    <h:outputLabel for="title" value="#{msgs.event_title}" />
                    <h:inputText id="title" value="#{editEventBean.event.title}"
                        required="true" requiredMessage="#{msgs.event_reqTitle}"
                        label="#{msgs.event_title}" size="74">
                        <f:validateLength minimum="2" maximum="70" />
                    </h:inputText>
                    <h:message for="title" errorStyle="color:red" />
                    <h:outputLabel for="description" value="#{msgs.event_description}" />
                    <h:inputTextarea id="description"
                        value="#{editEventBean.event.description}" required="true"
                        requiredMessage="#{msgs.event_reqDescription}"
                        label="#{msgs.event_description}" cols="60">
                        <f:validateLength minimum="2" maximum="500" />
                    </h:inputTextarea>
                    <h:message for="description" errorStyle="color:red" />
                    <h:outputLabel for="location" value="#{msgs.event_location}" />
                    <h:inputText id="location" value="#{editEventBean.event.location}"
                        required="true" requiredMessage="#{msgs.event_reqLocation}"
                        label="#{msgs.event_location}" size="50">
                        <f:validateLength minimum="2" maximum="50" />
                    </h:inputText>
                    <h:message for="location" errorStyle="color:red" />


                    <h:outputLabel for="startOfEvent"
                        value="#{msgs.event_editStartDate}" />
                    <h:inputText id="startOfEvent"
                        value="#{editEventBean.event.startOfEvent}" required="true"
                        requiredMessage="#{msgs.event_reqStartDate}"
                        label="#{msgs.event_startDate}" size="50">
                        <f:convertDateTime pattern="#{msgs.longDate}" />
                    </h:inputText>
                    <h:message for="startOfEvent" errorClass="error" />
                    <h:outputLabel for="endOfEvent" value="#{msgs.event_editEndDate}" />
                    <h:inputText id="endOfEvent" 
                        value="#{editEventBean.event.endOfEvent}" required="true"
                        requiredMessage="#{msgs.event_reqEndDate}"
                        label="#{msgs.event_endDate}" size="50">
                        <f:convertDateTime pattern="#{msgs.longDate}" />
                    </h:inputText>
                    <h:message for="endOfEvent" errorClass="error" />

                    <h:outputLabel for="ticketAmount"
                        value="#{msgs.event_ticketAmount}" />
                    <h:inputText id="ticketAmount"
                        value="#{editEventBean.event.entireTicketamount}" required="true"
                        requiredMessage="#{msgs.event_reqTicketAmount}"
                        label="#{msgs.event_ticketAmount}" size="50">
                        <f:convertNumber integerOnly="true" />
                    </h:inputText>
                    <h:message for="ticketAmount" errorStyle="color:red" />
                    <h:outputLabel for="ticketPrice" value="#{msgs.event_ticketPrice}" />
                    <h:inputText id="ticketPrice"
                        value="#{editEventBean.event.ticketPrice}" required="true"
                        requiredMessage="#{msgs.event_reqTicketPrice}"
                        label="#{msgs.event_ticketPrice}" size="50">
                        <f:convertNumber type="currency" currencySymbol="&#8364;"
                            maxFractionDigits="2" />
                    </h:inputText>
                    <h:message for="ticketPrice" errorStyle="color:red" />
                    <h:outputLabel for="accountNumber"
                        value="#{msgs.event_accountNumber}" />
                    <h:inputText id="accountNumber"
                        value="#{editEventBean.event.accountNumber}" required="true"
                        requiredMessage="#{msgs.event_reqAccountNumber}"
                        label="#{msgs.event_accountNumber}" size="50">
                        <f:convertNumber integerOnly="true" />
                    </h:inputText>
                    <h:message for="accountNumber" errorStyle="color:red" />
                    <h:outputLabel for="cashPayment" value="#{msgs.event_cashPayment}" />
                    <h:selectBooleanCheckbox id="cashPayment"
                        value="#{editEventBean.event.cashPayment}" />
                    <h:outputLabel></h:outputLabel>
                    <h:outputLabel for="creditPayment"
                        value="#{msgs.event_creditPayment}" />
                    <h:selectBooleanCheckbox id="creditPayment"
                        value="#{editEventBean.event.creditPayment}" />
                    <h:outputLabel></h:outputLabel>
                    <h:outputLabel for="isActive" value="#{msgs.event_isActive}" />
                    <h:selectBooleanCheckbox id="isActive"
                        value="#{editEventBean.event.isActive}" />
                    <h:outputLabel></h:outputLabel>
                    <h:outputLabel for="isActive"
                        value="#{msgs.event_chooseTicketColor}" />
                    <h:selectOneMenu value="#{editEventBean.color}">
                        <f:selectItem itemValue="PINK" itemLabel="Pink" />
                        <f:selectItem itemValue="BLUE" itemLabel="Blue" />
                    </h:selectOneMenu>
                    <h:outputLabel></h:outputLabel>
                    <h:commandButton id="updateButton" value="#{msgs.event_update}"
                        action="#{editEventBean.update(editEventBean.eventID)}"
                        binding="#{editEventBean.updateButton}" />
                    <h:message for="updateButton" infoClass="info" />
                </h:panelGrid>
                <h:panelGrid columns="4">
                    <h:outputLabel for="verificationCode" value="#{msgs.event_vc}" />
                    <h:inputText id="verificationCode"
                        value="#{editEventBean.event.verificationCode}" readonly="true">
                    </h:inputText>
                    <h:commandButton id="generateVerCode"
                        value="#{msgs.event_generateVC}"
                        action="#{editEventBean.generateNewVerificationCode(editEventBean.eventID)}"
                        binding="#{editEventBean.generateVerCode}" />
                    <h:message for="generateVerCode"
                        infoStyle="color:darkgreen; font-weight: bold;" />
                </h:panelGrid>
            </f:view>
        </h:form>
        <br />
    </ui:define>
</ui:composition>

发现我的错误。这就像令人沮丧一样简单...这

h:form enctype="multipart/form-data"

需要一个身份证。我需要上传文件的 enctype。然后表单必须有一个 id...两个页面帮助我找到了这个错误:

"7.如果带有 UICommand 按钮的父级事先被 ajax 请求渲染/更新,那么第一个操作将始终失败。(命令按钮/命令链接/ajax 操作/侦听器方法未调用或输入值未更新)

以及使用文件上传的示例:

http://jsflive.wordpress.com/2013/04/23/jsf22-file-upload/

如果代码不是你自己的,就很难找到这么小的错误......

最新更新