【问题标题】:JSF2.2 @Named does not workJSF2.2 @Named 不起作用
【发布时间】:2014-06-11 13:19:31
【问题描述】:

我有一个简单的 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 示例会很棒。

【问题讨论】:

    标签: java maven annotations jetty jsf-2.2


    【解决方案1】:

    您可以尝试清理项目并重新运行它。有时代码更改不会通过 ide 正确更新。 添加一个 cdi 范围,如 @ViewScoped (import javax.faces.view.ViewScoped;),然后重试。

    更新: 我看到您正在使用码头作为网络容器。我搜索jetty cdi,发现jetty默认不支持CDI。 Weld 可用于添加对 CDI 的支持:

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

    Deploying a war to Jetty with CDI

    【讨论】:

    • 感谢您的回复,但这并没有改变。我添加了@ViewScoped,重新导入了 maven 依赖项,重建了项目,但我的 index.xhtml 中仍然没有任何价值。
    猜你喜欢
    • 2014-03-18
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 2018-05-08
    • 1970-01-01
    • 2012-06-15
    • 2014-04-05
    相关资源
    最近更新 更多