JSF Managed Bean没有被调用

时间:2014-06-02 11:08:41

标签: jsf jboss

我开始使用JSF并在Eclispe中尝试以下教程: 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类,它没有返回任何输出。如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

outputText组件包含在h:form标记内:

<h:form>
   <h:outputText value="#{hello.message}"/> 
</h:form>

您还可以选择省略outputText所有内容,只在页面中放置EL expression,它就可以正常使用:

#{hello.message}

答案 1 :(得分:0)

JSF 2.2有新的命名空间,可能是因为你的xhtml没有渲染组件。你能否修改xhtml中的namepsace,如下所示,试试看,

<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>