我正在尝试在JSF中创建一个表单以将事件添加到我的数据库中。我遇到的问题是bean属性没有更新,因此我总是得到空值。我想念的是什么,或者我做错了什么。
我尝试使用h:commandButton
代替p:commandButton
,但是结果是一样的。我还尝试过将bean的作用域设置为会话和请求,也无济于事。
这是JSF页面。
<?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>
托管bean:
package beans;
import dao.EventCategoryDAO;
import dao.EventPostDAO;
import dao.PostDAO;
import model.EventCategory;
import model.EventPost;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@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());
System.out.println("called");
}
public void setToday(Date today) {
this.today = today;
}
public List<EventCategory> getEventCategories() {
return eventCategories;
}
public void setEventCategories(List<EventCategory> eventCategories) {
this.eventCategories = eventCategories;
}
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 a new event.";
name = "";
description = "";
pictureURL = "";
category = 0;
allEvents.add(0, post);
} else {
message = "Error.";
}
return null;
}
private String mapContains(Map<String, String> map, List<String> keys) {
for (String key : keys) {
System.out.println(key + " - " + map.get(key));
if (!map.containsKey(key)) {
return "Greska. Niste unijeli " + key;
}
}
return "OK";
}
public int getCategory() {
return category;
}
public void setCategory(int category) {
this.category = category;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getName() {
System.out.println("get: " + name);
return name;
}
public void setName(String name) {
System.out.println("set: " + name);
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPictureURL() {
return pictureURL;
}
public void setPictureURL(String pictureURL) {
this.pictureURL = pictureURL;
}
public List<EventPost> getAllEvents() {
return allEvents;
}
public void setAllEvents(List<EventPost> allEvents) {
this.allEvents = allEvents;
}
public String updateDeleted() {
Map<String, String> requestParamMap =
FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
if (requestParamMap.containsKey("eventId") && requestParamMap.containsKey("deleted")) {
int eventId = Integer.parseInt(requestParamMap.get("eventId"));
boolean deleted = Boolean.parseBoolean(requestParamMap.get("deleted"));
PostDAO.updateDeleted(eventId, deleted);
int index = allEvents.indexOf(new EventPost(eventId));
if (index >= 0) {
allEvents.get(index).setDeleted(deleted);
}
}
return null;
}
}
出于明显的原因,此代码段中排除了get和set方法,以免造成混乱。
我希望在用户输入一些值时更新这些值,以便在调用方法addEvent时,这些值将更新并且不为null,但是我得到的都是空值。
编辑:我已经更新了答案,它包含了整个bean。
编辑2:到目前为止,没有发生任何异常,而我没有更改上面提供的代码。 Stacktrace如下:
06-Apr-2019 12:57:06.032 SEVERE [http-nio-8081-exec-50] com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError javax.faces.application.ViewExpiredException: viewId:/pages/body_add_event.xhtml - View /pages/body_add_event.xhtml could not be restored.
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:210)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:121)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:844)
06-Apr-2019 12:57:06.044 SEVERE [http-nio-8081-exec-49] com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError javax.faces.application.ViewExpiredException: viewId:/pages/body_add_event.xhtml - View /pages/body_add_event.xhtml could not be restored.
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:210)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:121)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:844)
答案 0 :(得分:0)
ViewException仅仅是因为会话已过期。重新加载应用程序,此错误将消失。
p:commandButton不仅应处理@this,因为它仅意味着commandButton本身。尝试例如process =“ @ form”。