Lou*_*ous 17 java java-ee java-ee-6 jsf-2 managed-bean
我想尝试一个简单的JSF 2教程示例.
我在Eclipse中使用动态Web项目并发布到Glassfish 3服务器(运行 - >在服务器上运行).第一个index.xhtml页面正确加载,但是当我必须访问托管bean时,会显示以下错误:
/index.xhtml @14,48 value="#{helloBean.name}": Target Unreachable, identifier 'helloBean' resolved to null
Run Code Online (Sandbox Code Playgroud)
我已经看过关于这个主题的各种其他讨论,但是这些解决方案对我来说似乎永远不会起作用(例如,添加beans.xml,根据命名约定给托管bean命名等).
任何帮助,将不胜感激.
这是我目前正在使用的代码:
的index.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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body>
<h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
<h:form>
<h:inputText value="#{helloBean.name}"></h:inputText>
<h:commandButton value="Welcome Me" action="response"></h:commandButton>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
response.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">
<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body bgcolor="white">
<h3>JSF 2.0 Hello World Example - welcome.xhtml</h3>
<h4>Welcome #{helloBean.name}</h4>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
管理豆:
package java.hello1;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {
private static final long serialVersionUID = 1L;
private String name = "Ricardo";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>JavaServerFaces</display-name>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
Bal*_*usC 14
您需要拥有一个JSF 2.0兼容的/WEB-INF/faces-config.xml文件才能让JSF解释注释.
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
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/web-facesconfig_2_0.xsd"
version="2.0">
<!-- Config here. Can even be kept empty. -->
</faces-config>
Run Code Online (Sandbox Code Playgroud)
如果您已经有一个或如果没有解决问题,请注意服务器启动日志,如果您没有看到任何警告/错误.
顺便说一句,您的/WEB-INF/web.xml文件声明符合Servlet 2.5规范.虽然这可能不一定有害,但如果您使用符合Servlet 3.0的容器则毫无意义.更新根声明如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
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/web-app_3_0.xsd"
version="3.0">
<!-- Config here. -->
</web-app>
Run Code Online (Sandbox Code Playgroud)
该/WEB-INF/beans.xml是intented为CDI注解一样@Named,@Inject等等.只需一个完全空的文件即可将其打开.它与JSF注释完全没有关系@ManagedBean,@ManagedProperty等等.它也不应该相互混淆/混合.
ale*_*hro 10
我已经被困了这个问题半天了.我的问题只有在我从Eclipse运行WebApp时才出现.JSF 2在WEB-INF/classes中查找带注释的bean,但找不到它们.为了解决这个问题,我将构建输出路径更改为WebContent/WEB-INF/classes.下面是类似案例的详细解释:Jetty maven目标jetty:run不能与JSF 2.0 @ManagedBean一起使用
| 归档时间: |
|
| 查看次数: |
46395 次 |
| 最近记录: |