【发布时间】:2016-11-24 02:57:22
【问题描述】:
这是我的代码:
pom.xml
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.13</version>
</dependency>
<build>
<finalName>darbe</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.1.v20140609</version>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds>
<webApp>
<contextPath>/</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>foo.xhtml</welcome-file>
</welcome-file-list>
</web-app>
foo.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Foo</title>
</h:head>
<h:body>
<h:outputText value="#{foo.bar}"/>
</h:body>
</html>
最后是 Foo.java
package biz.tugay;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class Foo {
private String bar;
@PostConstruct
public void init() {
bar = "Hello World";
}
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
所以这里我使用的是 Oracle 的 JSF 实现。当我构建这个项目并将其部署到 Tomcat 或使用 Jetty 插件(mvn jetty:start)时,我会在浏览器上看到文本 Hello World。
但是,当我更改依赖项时,如下所示:
<dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-api</artifactId>
<version>2.2.10</version>
</dependency>
<dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-impl</artifactId>
<version>2.2.10</version>
</dependency>
如果我使用 maven 构建 war 文件并将其部署到 Tomcat,我只会看到 Hello World。当我使用 mvn jetty:start 运行应用程序时,@PostConstruct 将永远不会被调用,因此 foo.bar 将为空。
这里发生了什么? javax.annotation 包中的 Annotation 如何根据我选择的 JSF 实现和运行 war 文件的容器处理/未处理?
【问题讨论】: