我正在开始使用JSF,并在eclipse中尝试以下教程:http://javing.blogspot.de/2013/01/how-to-create-new-jsf-21-project-and.html
我已经让它工作,以便它在JBoss EAP 6上编译和部署没有错误。但是我没有从我的托管bean类中获得任何输出,它只包含一个简单返回文本字符串的函数。以下是代码文件:
web . xml
<?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">
<display-name>Archetype Created Web Application</display-name>
<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>
<welcome-file-list>
<welcome-file>home.xhtml</welcome-file>
</welcome-file-list>
</web-app>
home.xhtml
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>www.javing.blogspot.com</title>
</h:head>
<h:body>
<h:outputText value="#{hello.message}"/>
</h:body>
</html>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org
/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zeeshan.www</groupId>
<artifactId>myJSFTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>JSF Test App</name>
<dependencies>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.6-jbossorg-2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
和托管Bean类
包myJSFTest;
import javax.faces.bean.ManagedBean;
@ManagedBean(name="hello")
public class Hello {
public String getMessage()
{
System.out.println("Hello world function called");
return "www.javing.blogspot.com";
}
}
我已经通过静态输出验证了它确实是不返回任何输出的托管bean类。如何解决这个问题?
将outputText
组件包含在h:form
标签中:
<h:form>
<h:outputText value="#{hello.message}"/>
</h:form>
你也可以选择忽略outputText
,只把EL expression
放在你的页面,它会工作得很好:
#{hello.message}
JSF 2.2
有新的名称空间,这可能是由于您的xhtml没有呈现组件。你能像下面这样修改xhtml中的命名空间并尝试一下吗?
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
</html>