I am trying to make a form in JSF to add an event to my database. The problem I am having is that the bean properties are not updated, and as such I am always getting null values. What am I missing, or what did I do wrong.
I have tried using h:commandButton
instead of p:commandButton
, but the result was the same. I have also tried setting the bean's scope to session and request, also to no avail.
This is the JSF page.
<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Event</title>
</h:head>
<h:body>
<ui:include src="/WEB-INF/components/header.xhtml"/>
<h:form>
<p:panel id="panel" header="Event" style="margin-bottom:10px; align-content: center">
<p:growl id="messages" showDetail="true"/>
<h:panelGrid columns="2" cellpadding="5">
<p:outputLabel value="Name: "><p:outputLabel style="color: red" value="*"/></p:outputLabel>
<p:inputText name="name" id="name" required="true" value="#{eventUtilBean.name}"/>
<p:outputLabel value="Description: "><p:outputLabel style="color: red" value="*"/></p:outputLabel>
<p:inputTextarea id="description" name="description" rows="8" cols="100" counter="display"
maxlength="1000" value="#{eventUtilBean.description}"
counterTemplate="{0} characters remaining." autoResize="true" required="true"/>
<br/>
<h:outputText id="display"/>
<p:outputLabel value="Time: "><p:outputLabel style="color: red"
value="*"/></p:outputLabel>
<p:calendar value="#{eventUtilBean.today}" mindate="#{eventUtilBean.today}" showOn="button"
pattern="dd/MM/yyyy HH:mm:ss"/>
<p:outputLabel value="Picture: "><p:outputLabel style="color: red" value="*"/></p:outputLabel>
<p:inputText name="picture" id="picture" required="true" value="#{eventUtilBean.pictureURL}"/>
<p:outputLabel value="Category: "><p:outputLabel style="color: red" value="*"/></p:outputLabel>
<p:selectOneMenu id="selectOneEventCategoryMenu" style="width:125px" value="#{eventUtilBean.category}">
<f:selectItems value="#{eventUtilBean.eventCategories}" var="eventCategory"
itemValue="#{eventCategory.id}" itemLabel="#{eventCategory.name}"/>
</p:selectOneMenu>
<p:commandButton action="#{eventUtilBean.addEvent}" process="@this" value="Add event">
<f:ajax render="message"/>
</p:commandButton>
</h:panelGrid>
<p:outputLabel id="message" value="#{eventUtilBean.message}"/>
</p:panel>
</h:form>
</h:body>
</html>
The managed bean:
@ManagedBean(name = "eventUtilBean")
public class EventUtilBean {
private Date today = new Date();
private List<EventCategory> eventCategories;
private int category;
private String message;
private String name;
private String description;
private String pictureURL;
private List<EventPost> allEvents;
private static int i = 0;
public Date getToday() {
return new Date();
}
@PostConstruct
private void init() {
eventCategories = EventCategoryDAO.getAll();
allEvents = EventPostDAO.getAllEventPosts(false);
allEvents.addAll(EventPostDAO.getAllEventPosts(true));
allEvents =
allEvents.stream().sorted((e1, e2) -> e2.getTimestamp().compareTo(e1.getTimestamp())).collect(Collectors.toList());
}
public String addEvent() {
System.out.println(name + " - " + description + " - " + pictureURL + " - " + category);
if (name == null || name.equals("") || description == null || description.equals("") || pictureURL == null || pictureURL.equals("") || category < 1) {
message = "Some fields were left empty.";
return null;
}
EventPost post = EventPostDAO.addEventPost(name, description, pictureURL, today, category);
if (post != null) {
message = "You have successfully added an event.";
name = "";
description = "";
pictureURL = "";
category = 0;
allEvents.add(0, post);
} else {
message = "Error.";
}
return null;
}
}
For obvious reasons, the get and set methods were excluded in this snippet, as not to clutter it.
I expect the values to be updated as the user enters some value, so that when the method addEvent is called the values are updated and not null, but all I am getting are null values.
Aucun commentaire:
Enregistrer un commentaire