I have a huge problem with my jsf page. I have to pass a Parameter in Post request to a request scoped Bean. All works fine except one little bug. The button to update my information reacts only from second click on. This Page has to be request scope. Everytime i push the button it first runs the @PostConstruct and is then ready for a click. I tried to delete the PostConstruct method but my program than stops. I tried nearly everything. Any ideas please?
Here is my Code: I enter this page by calling the Methode "showEditEvent" from another Bean.
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() + ".");
}
And here is my Facelet
<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="€"
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>