我在JSF 2.2中遇到一个简单的Web应用程序问题,我的托管bean没有解决,我收到了这个错误:
/greeting.xhtml @21,66 date="#{dukesBDay.yourBD}": Target Unreachable, identifier 'dukesBDay' resolved to null
这是我的托管bean:
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class DukesBDay {
@EJB
private DukesBirthDayBean dukesBirthdayBean;
这是我的greeting.xhtml:
<?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://java.sun.com/jsf/html"
xmlns:fc="http://java.sun.com/jsf/composite/components">
<h:head>
<title>First Cup Greeting Page</title>
</h:head>
<h:form>
<h2>
<h:outputText value="#{bundle.Welcome}"/>
</h2>
<h:outputText value="#{bundle.DukeIs} "/>
<h:outputText value="#{dukesBDay.age} #{bundle.YearsOldToday}"/>
<p/>
<h:outputText value="#{bundle.Instructions}"/>
<p/>
<h:outputText value="#{bundle.YourBD} "/>
<fc:inputDate id="userBirthday" date="#{dukesBDay.yourBD}" />
<h:outputText value=" #{bundle.Pattern}"/>
<p/>
<h:commandButton value="#{bundle.Submit}"
action="#{dukesBDay.processBirthday}"/>
<p/>
<h:message for="userBirthday" style="color:red"/>
</h:form>
这是我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>greeting.xhtml</welcome-file>
</welcome-file-list>
</web-app>
我尝试将name属性添加到@ManagedBean:
package firstcup.web;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="dukesBDay")
@SessionScoped
public class DukesBDay {
但它不起作用,仍然有相同的错误。
我尝试在faces-config中映射托管bean:
<managed-bean>
<managed-bean-name>dukesBDay</managed-bean-name>
<managed-bean-class>firstcup.web.DukesBDay</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
但是我收到了这个错误:
无法创建托管bean dukesBDay。发现以下问题: - 无法找到Bean或属性类firstcup.web.DukesBDay for managed bean dukesBDay
为什么我的DukesBDay没有解决?怎么了?
由于
答案 0 :(得分:2)
我解决了重启NetBeans和GlassFish Server的问题。
我没有将name属性添加到@ManagedBean:
package firstcup.web;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class DukesBDay {
并且一切正常,因此没有必要(根据命名约定,您可以使用类的非大写短名称来引用bean)
答案 1 :(得分:0)
编写@ManagedBean时,还应该为其添加name属性。
@ManagedBean(name="dukesBDay")
@sessionscoped
使用faceconfig.xml文件声明托管bean的另一种方法。
<managed-bean>
<managed-bean-name>dukesBDay</managed-bean-name>
<managed-bean-class>package.DukesBDay</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
如果你没有指定dukesBDay和@ManagedBean,它将被解析为null。
答案 2 :(得分:0)
在答案中:https://stackoverflow.com/a/28142131/7339033 @Named
标记未显示,但对我来说只插入@ManagedBean
标记解决了问题
@Named(value = "DukesBDay")
@ManagedBean
@SessionScoped
public class DukesBDay implements Serializable {
@EJB
private DukesBirthdayBean dukesBirthdayBean;
protected int age;
//@NotNull (In my case I'm using Glassfish 5)
protected Date yourBD;
...
通过这种方式,您无需更改其他代码中的名称。