我非常简单的 Web 应用程序有什么问题:Web 应用程序成功部署到应用程序服务器但 hello bean 没有注入(inject)到 index.xhtml 页面
(页面仅显示来自 Facelets 的 Hello:#{hello.value})?
(这是我第一次使用 JSF,所以这个问题可能很简单,而且我使用了很好的文章 http://arjan-tijms.omnifaces.org/2011/08/minimal-3-tier-java-ee-app-without-any.html )
我有 war 文件的下一个结构:
mywebapp
|
- WEB_INF
|
- classes
|
- Hello.class
- index.html
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;
}
}
还有我的 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>
对于构建项目,我使用了 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>
最佳答案
取自 JavaServerFaces 2.0,完整引用 :
An entry in the Web application’s web.xml file enables the Faces Controller servlet when a certain URL pattern is specified, such as /faces/. When running JSF 2.0 on a Servlet 3.0 container, such as Sun’s Glassfish v3, the web.xml is optional. If no web.xml is found, the Faces Controller servlet is automatically mapped to the most popular URL patterns: /faces/, .jsf, and .faces.
所以你应该尝试这样的事情:
localhost:8080/mywebapp/faces/index.xhtml
.
关于jsf - 如何在没有 web.xml 的 Java EE 7 环境中运行 JSF 2.2 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25834308/