Я пытаюсь реализовать простую гостевую книгу.javax.el.PropertyNotFoundException: Target Unreachable, идентификатор 'unregisteredUserPost' разрешен к нулю
Когда пользователь нажимает кнопку «Отправить», то UnregisteredUserPost
КДИ боб должен быть создан. Однако, вместо этого я получаю следующее исключение:
javax.servlet.ServletException: /guestbook.xhtml @11,57 value="#{unregisteredUserPost.name}": Target Unreachable, identifier 'unregisteredUserPost' resolved to null
Я appreaciate, если вы поможете мне найти причину проблемы.
guestbook.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>#{msg.page_title}</title>
<link rel="stylesheet" type="text/css" href="css.css"/>
</h:head>
<h:body>
<h:form>
<h:outputText value="#{msg.your_name} "/>
<h:inputText value="#{unregisteredUserPost.name}"/>
<br/><br/>
<h:outputText value="#{msg.your_msg}"/>
<br/>
<h:inputTextarea
rows="5" cols="100" value="#{unregisteredUserPost.content}"/>
<br/><br/>
<h:commandButton
value="#{msg.submit}" action="#{unregisteredUserPost}"/>
</h:form>
</h:body>
</html>
UnregisteredUserPost.java
package learning.javaee.guestbook;
import java.time.OffsetDateTime;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@Named
@SessionScoped
public class UnregisteredUserPost extends AbstractPost {
private String name;
private OffsetDateTime dateTime;
public UnregisteredUserPost() {}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public OffsetDateTime getDateTime() {
return dateTime;
}
public void setDateTime(OffsetDateTime dateTime) {
this.dateTime = dateTime;
}
}
AbstractPost.java
package learning.javaee.guestbook;
import java.io.Serializable;
import java.util.logging.Logger;
import javax.inject.Inject;
import javax.inject.Named;
import javax.interceptor.AroundConstruct;
import javax.interceptor.InvocationContext;
@Named
public abstract class AbstractPost implements Serializable {
private String content;
@Inject
private static Logger log;
@AroundConstruct
private void logConstruction(InvocationContext ic) {
try {
ic.proceed();
log.fine("Created " + getClass().getName());
} catch (Exception e) {
log.severe(e.toString());
}
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1">
<display-name>guestbook1</display-name>
<welcome-file-list>
<welcome-file>guestbook.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
лица-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<locale-config>
<default-locale>en</default-locale>
</locale-config>
<resource-bundle>
<base-name>MessagesBundle</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
выглядит не так уж плохо. Две вещи, которые я вижу. 1. Почему ваш bean SessionScoped? Вы хотите обработать состояние экземпляра самостоятельно? Сделать это событие или просмотреть область охвата достаточно для формы. Потому что вам нужен новый экземпляр для пустой формы. 2. Что должно произойти с действием = "unregisteredUserPost" на кнопке? Вам нужно передать метод атрибуту action, может быть bean-компонентом, который может сохранить сообщение;) Остальное в порядке, у вас есть beans.xml для активации CDI, вы определяете webprofile 3.1 и jsf 2.2, которые все в порядке. –