index.xhtml的<h:outputtext>未在浏览器中呈现</h:outputtext>

时间:2015-01-17 11:19:01

标签: jsf xhtml

我是JAVA EE的新手。我的问题是标签h:outputText没有在浏览器中呈现。我用

  1. Eclipse JAVA EE IDE(Luna Service Release 1(4.4.1))
  2. Apache的Tomcat的7.0.57
  3. Maven 3.2.5
  4. Java版本1.7
  5. 库中包含的JAR文件: junit3.8.1,jsf-api-2.1.13,jsf-impl-2.1.12,javax.servlet-api-3.0.1

    以下是我的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_3_0.xsd"
     id="WebApp_ID" version="3.0">
        <display-name>jsfexample</display-name>
        <servlet>
            <servlet-name>faces</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>faces</servlet-name>
            <url-pattern>*.xhtml</url-pattern>
       </servlet-mapping>
       <welcome-file-list>
            <welcome-file>index.xhtml</welcome-file>
        </welcome-file-list> 
    </web-app>
    

    的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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.javacodegeeks.snippets.enterprise</groupId>
      <artifactId>jsfexample</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>jsfexample Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
            <dependency>
                <groupId>com.sun.faces</groupId>
                <artifactId>jsf-api</artifactId>
                <version>2.1.13</version>
            </dependency>
            <dependency>
                <groupId>com.sun.faces</groupId>
                <artifactId>jsf-impl</artifactId>
                <version>2.1.13</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.0.1</version>
            </dependency>
      </dependencies>
      <build>
        <finalName>jsfexample</finalName>
      </build>
    </project>
    

    的index.xhtml

    <!DOCTYPE html>
    <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>JavaCodeGeeks</title>
    </h:head>
    <h:body>
        <b>Check B tag</b>
        <h:outputText value="#{helloWorldBean.msg}" />
    </h:body>
    </html>
    

    JAVABEAN-helloWorld.java

    import javax.annotation.PostConstruct;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;
    @ManagedBean(name="helloWorldBean")
    @RequestScoped
    public class HelloWorldBean {
        public String msg="test";
        public String getMsg() {
            return msg;
        }
        public void setMsg(String msg) {
           this.msg = msg;
        }
        @PostConstruct
        private void init() {
            msg = "Hello World!! JFS example.. ";
       }
    }
    

    请告诉我为什么标签没有渲染。提前谢谢。

1 个答案:

答案 0 :(得分:-2)

事实上,将servlet映射放在web.xml中更好,如下所示

<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>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>

因此,您告诉您的应用程序,任何名为wirh prefix faces /的xhtml文件都应该由面Servlet呈现。