@Named与@ManagedBean注释

时间:2015-01-17 11:32:07

标签: java java-ee annotations

我已经阅读了其他帖子,但我仍然没有得到它。我有这门课:

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时,我可以在浏览器中调用该页面,但是当我点击“欢迎我”时,我收到以下错误:

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

我何时使用哪个注释?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

检查您的进口。 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