JSF2.2 @Named不起作用

时间:2014-06-11 13:19:31

标签: java maven annotations jetty jsf-2.2

我有一个简单的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();
    }

}

这就是视图:

<?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 - 没有@ManagedBean的jsf2.2示例会很棒。

1 个答案:

答案 0 :(得分:1)

您可以尝试清理项目并重新运行它。有时,ide不能正确更新代码更改。 添加像 @ViewScoped 这样的cdi范围(导入javax.faces.view.ViewScoped;)然后重试。

更新 我看到你使用jetty作为web容器。我搜索jetty cdi并发现jetty默认不支持CDI。 Weld可用于添加对CDI的支持:

http://www.eclipse.org/jetty/documentation/current/framework-weld.html

Deploying a war to Jetty with CDI