This question already has answers here:
Java EE 6 @javax.annotation.ManagedBean vs. @javax.inject.Named vs. @javax.faces.ManagedBean
                                
                                    (3个答案)
                                
                        
                                2年前关闭。
            
                    
我已经读过其他文章,但我仍然不明白。我有这个课:

import java.util.List;

import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;

@ManagedBean
@RequestScoped
public class ManagedBean{

    private UserService userService;

    @Inject
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    private User user;
    private List<User> list;

    public ManagedBean() {
    }

    @PostConstruct
    public void init() {
        user = userService.returnUserById(1);
        list = userService.returnAllUsers();
    }

    public User getUser() {
        return user;
    }

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

    public List<User> getList() {
        return list;
    }

    public void setList(List<User> list) {
        this.list = list;
    }
}


然后这个课:

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named
@SessionScoped
public class TestBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private int num = 6;
    private String name;

    public String getName() {
        return name;
    }

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

    public int getNum() {
        return num ;
    }

    public void setNum(int num) {
        this.num = num;
    }

}


然后这个.xhtml页面:

<!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://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head />
<h:body>
        TEST
        Welcome #{testBean.num}

        <h:form>
        <h:inputText value="#{testBean.name}"></h:inputText>
        <h:commandButton value="Welcome Me" action="xhtml/second"></h:commandButton>
    </h:form>

    <h3>Return All Users</h3>

    <h:dataTable value="#{homeManagedBean.list}" var="user">
        <h:column>
            <h:outputText
                value="User Id= #{user.userId} + User Name = #{user.userName}" />
        </h:column>
    </h:dataTable>

    <h3>Return User By Id 2</h3>

    <h:outputText value="User Id #{homeManagedBean.user.userId}" />
    <h:outputText value="User Name #{homeManagedBean.user.userId}" />
</h:body>
</html>


当我在第一类中将@ManagedBean更改为@Named时,我从glassfish中得到以下错误:

WELD-000049 Unable to invoke public void a.ManagedBean.init() on a.ManagedBean@3b26b9ca


当我在第二个类中将@Named更改为@ManagedBean时,可以在浏览器中调用该页面,但是当我单击“ Welcome me”时,出现以下错误:

/index.xhtml @16,41 value="#{testBean.name}": Target Unreachable, identifier 'testBean' resolved to null


什么时候使用哪个注释?

感谢帮助!

最佳答案

检查您的导入。 CDI和JSF使用来自不同导入的相同作用域注释。您可以在javax.enterprise.context.*中找到CDI范围(@Named),并在javax.faces.bean.*中找到JSF范围(@ManagedBean)。

@ManagedBean批注也可以从不同的导入中获得

检查这篇文章:
Java EE 6 @javax.annotation.ManagedBean vs. @javax.inject.Named vs. @javax.faces.ManagedBean

10-08 09:25