如何在没有web.xml的Java EE 7环境中运行JSF 2.2页面?

Dim*_*kyi 5 jsf web.xml jsf-2.2 java-ee-7

我的非常简单的Web应用程序出了什么问题:Web应用程序成功部署到app服务器但是hello bean没有注入index.xhtml页面(页面显示来自Facelets的Hello:#{hello.value})?

(这是我第一次使用JSF,所以也许这个问题很容易,而且我也使用了很好的文章http://arjan-tijms.omnifaces.org/2011/08/minimal-3-tier-java- ee-app-without-any.html)

我有下一个战争档案结构:

mywebapp
|
 - WEB_INF
  |
   - classes
     |
      - Hello.class
 - index.html
Run Code Online (Sandbox Code Playgroud)

Hello.java有:

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class Hello {

    private String value;

    public String getValue() {
        return "Hello JSF";
    }

    public void setValue(String value) {
        this.value = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

还有我的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:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>My Facelet Page Title</title>
    </h:head>
    <h:body>
        Hello from Facelets: #{hello.value}
    </h:body>
</html>
Run Code Online (Sandbox Code Playgroud)

对于构建项目,我使用了pom.xml:

....
<packaging>war</packaging>
<name>Simple web app</name>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

Aru*_*pta 5

根据javadoc,FacesServlet如果满足以下任一条件,将自动注册:

  • faces-config.xml找到一个文件WEB-INF
  • 在应用程序的类路径中的jar目录中faces-config.xml找到一个文件META-INF.
  • 结尾的文件名位于应用程序类路径中jar .faces-config.xmlMETA-INF目录中.
  • javax.faces.CONFIG_FILES情境参数是在声明web.xmlweb-fragment.xml.
  • Set传递给类onStartup()的方法ServletContainerInitializer执行不为空.

如果您web.xml仅用于注册FacesServlet,那么如果满足上述任何条件,它将是可选的.


Sei*_*dis 5

摘自JavaServerFaces 2.0,完整参考:

当指定某个URL模式时,Web应用程序的web.xml文件中的条目启用Faces Controller servlet,例如/ faces /.在Servlet 3.0容器(例如Sun的Glassfish v3)上运行JSF 2.0时,web.xml是可选的.如果未找到web.xml,则Faces Controller servlet会自动映射到最常用的URL模式:/ faces /,.jsf和.faces.

所以你应该尝试这样的事情:

localhost:8080/mywebapp/faces/index.xhtml.