【问题标题】:@ManagedProperty does not work in a CDI managed bean@ManagedProperty 在 CDI 托管 bean 中不起作用
【发布时间】:2023-03-07 23:42:01
【问题描述】:

我尝试学习 JSF 时遇到了一个与 ManagedProperty 相关的问题。但是我尝试使用它,它总是失败 - 空异常指针。我做错了什么? 我在 stackoverflow 上阅读了一些“类似的帖子”,但它们对我没有帮助。 (我使用 GlassFish 4.0、JSF 2.2、JDK 1.7、Netbeans 7.3.1(Java EE 包)和 Java EE 6.0)

<?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>Facelet Title</title>
    </h:head>
    <h:body>
    Hello from Facelets
    <br/>
    User: #{books.user.name}<br/>
    1: #{param.pageId}<br/>
    2: #{books.pageId}<br/>
    <h:form>
        <h:inputText value="#{user.name}" /><br/>
        <h:inputText value="#{books.v1}" /><br/>
        <h:inputText value="#{books.v2}" /><br/>
        <h:inputText value="#{books.result}" /><br/>
        <h:commandButton value="dodaj" action="#{books.add}" />
    </h:form>
    </h:body>
</html>

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tpsa.books.managed;

import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedProperty;

/**
 *
 * @author tomasz
 */
@Named(value = "books")
@RequestScoped
public class Books {

    private int v1;
    private int v2;
    private int result;
    @ManagedProperty(value = "#{user}")
    private User user;

    @ManagedProperty(value="#{param.pageId}")
    private int pageId;

    /**
     * Creates a new instance of Books
     */
    public Books() {
    }

    public void add() {
    result = v1 + v2;

    }

    public int getV1() {
    return v1;
    }

    public void setV1(int v1) {
    this.v1 = v1;
    }

    public int getV2() {
    return v2;
    }

    public void setV2(int v2) {
    this.v2 = v2;
    }

    public int getResult() {
    return result;
    }

    public void setResult(int result) {
    this.result = result;
    }

    public User getUser() {
    if (user == null) {
        System.err.println("WTF");
    }
    return user;
    }

    public void setUser(User user) {
    this.user = user;
    }

    public int getPageId() {
    return pageId;
    }

    public void setPageId(int pageId) {
    this.pageId = pageId;
    }



}

用户

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tpsa.books.managed;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;

/**
 *
 * @author tomasz
 */
@Named(value = "user")
@SessionScoped
public class User implements Serializable {

    private String name;

    /**
     * Creates a new instance of User
     */
    public User() {
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }



}

【问题讨论】:

    标签: jsf null cdi managed-property


    【解决方案1】:

    @ManagedProperty 是托管 bean 注释,不能与 CDI 一起使用。在上面的代码中,您使用了 CDI bean,即 @Named,这是 JSF 2.2 中的默认值。在这种情况下,您不能使用 ManagedProperty。请阅读从 ManagedBean 的 Java EE 文档复制的以下行。

    如果这个注解出现在一个没有 ManagedBean 注解,实现必须对此不采取任何行动 注释。

    详情见链接:

    http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedProperty.html

    因此,对于 CDI bean,请使用 @Inject 而不是 @ManagedProperty

    @Inject
    private User user;
    

    注意这里不需要getter/setter。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-20
    • 2012-08-27
    • 2016-07-09
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    相关资源
    最近更新 更多