我有一个简单的 Maven jsf2.2 项目,我使用 Maven jetty 插件执行该项目。
我有以下 bean:
package de.swp14i;
import javax.inject.Named;
//import javax.faces.bean.ManagedBean;
@Named
//@ManagedBean
public class BeanOne {
private String varFromBeanOne = "BeanOne";
public String getVarFromBeanOne() {
return this.varFromBeanOne;
}
public void init() {
System.out.println();
}
}
这就是 View :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html" >
<h:head>
<title>JSF 2.2 HTML5</title>
</h:head>
<h:body>
<h1>Blub</h1>
<h:outputText id="beanOne" value="#{beanOne.varFromBeanOne}"></h:outputText>
</h:body>
</html>
如果我启动jetty并打开index.xhtml,它就会被渲染,但是varFromBeanOne值不会显示,并且我的bean的init函数不会在我启动jetty的控制台中打印出任何内容。所以我猜它根本没有被调用。
如果我将 @Named 更改为 @ManagedBean 一切正常。
这是我的 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">
<!-- pom.xml specification version -->
<modelVersion>4.0.0</modelVersion>
<!-- project settings -->
<groupId>de.swp14i</groupId>
<artifactId>example</artifactId>
<version>0.1</version>
<name>example</name>
<packaging>war</packaging>
<properties>
<encoding>UTF-8</encoding>
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<jetty-maven-plugin.version>9.2.0.M1</jetty-maven-plugin.version>
</properties>
<!-- project module dependencies -->
<dependencies>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<!-- project maven plugins -->
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-maven-plugin.version}</version>
<configuration>
<scanIntervalSeconds>1</scanIntervalSeconds>
<webApp>
<contextPath>/</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
我不知道为什么会发生这种情况。但我需要 JSF2.2 来实现 CDI1.2。提示为什么它不起作用或者链接到一个工作的 maven - jsf2.2 示例(没有 @ManagedBean)会很棒。
最佳答案
您可以尝试清理项目并重新运行它。有时 ide 无法正确更新代码更改。 添加一个 cdi 范围,例如 @ViewScoped (import javax.faces.view.ViewScoped;),然后重试。
更新: 我看到您正在使用 jetty 作为网络容器。我搜索jetty cdi,发现jetty默认不支持CDI。 Weld 可用于添加对 CDI 的支持:
http://www.eclipse.org/jetty/documentation/current/framework-weld.html
关于java - JSF2.2 @Named 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24164019/