简体   繁体   English

JSF托管bean EJB注入

[英]JSF managed-bean EJB injection

I have an EJB (PersonManager) in the Enterprise Application modul, which injects another EJB (Person): 我在企业应用程序模块中有一个EJB(PersonManager),它注入另一个EJB(Person):

@Stateful
public class PersonManager implements PersonManagerLocal {
    @EJB
    private PersonLocal person;

    @Override
    public void setPersonName(String name) {
        person.setName(name);
    }

    @Override
    public String getPersonName() {
        return person.getName();
    }
}

I want to use the PersonManager EJB in a JSF web app. 我想在JSF Web应用程序中使用PersonManager EJB。 I define it in the faces-config.xml: 我在faces-config.xml中定义它:

<managed-bean>
    <managed-bean-name>personManager</managed-bean-name>
    <managed-bean-class>ejb.PersonManager</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

The problem is that, the injection of the PersonLocal EJB doesn't happen. 问题是,不会发生PersonLocal EJB的注入。 The person property is always NULL. person属性始终为NULL。 What did I wrong? 我错了什么?

But if I inject the PersonManager in a JSF managed bean like this: 但是如果我在这样的JSF托管bean中注入PersonManager:

@ManagedBean
@RequestScoped
public class Index {
    @EJB
    private PersonManagerLocal personManager;
    ....

IT WORKS. 有用。 I need the first scenario, please help me :-D 我需要第一个场景,请帮助我:-D

You are mixing the responsibilities of EJBs and JSF managed beans. 您正在混合EJB和JSF托管bean的职责。 The faces-config.xml registers only JSF artifacts, such as managed beans and not EJBs. faces-config.xml仅注册JSF工件,例如托管bean而不是EJB。 Your registration in faces-config.xml 您在faces-config.xml注册

<managed-bean>
    <managed-bean-name>personManager</managed-bean-name>
    <managed-bean-class>ejb.PersonManager</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

does basically exactly the same as 基本上完全相同

@ManagedBean
@SessionScoped
public class PersonManager {
    // ...
}

In other words, you're registering the class as a JSF managed bean which is available in views by #{personManager} . 换句话说,您将该类注册为JSF托管bean,该视图在#{personManager}视图中可用。 This does not represent the same instance as is managed by the EJB container. 这不代表与EJB容器管理的实例相同的实例。 You can and should not use faces-config.xml to register EJBs. 您可以也不应该使用faces-config.xml来注册EJB。 There you use the annotations from the javax.ejb package for, such as @Stateless and @Stateful . 在那里,您可以使用javax.ejb包中的注释,例如@Stateless@Stateful That's all you need to register an EJB. 这就是注册EJB所需的全部内容。

Actually, registering JSF managed beans in faces-config.xml is an old JSF 1.x way which has in JSF 2.x been replaced by the new @ManagedBean annotation. 实际上,在faces-config.xml注册JSF托管bean是一种旧的JSF 1.x方式,它在JSF 2.x中被新的@ManagedBean注释取代。


Update the proper approach would be: 更新正确的方法是:

View (the Facelets file): 查看(Facelets文件):

<h:form>
    <h:inputText value="#{personManager.person.name}" />
    ...
    <h:commandButton value="Save" action="#{personManager.save}" />
    <h:messages />
</h:form>

Controller (the JSF managed bean): Controller(JSF托管bean):

@ManagedBean
@ViewScoped
public class PersonManager implements Serializable {

    private Person person;

    @EJB
    private PersonService personService;

    @PostConstruct
    public void init() {
        person = new Person();
    }

    public void save() {
        personService.create(person);
        FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage("Person successfully created, new ID is " + person.getId()));
    }

    // ...
}

Model (the JPA entity): 模型(JPA实体):

@Entity
public class Person implements Serializable {

    @Id
    private Long id;

    private String name;

    // ...
}

Service (the stateless EJB): 服务(无状态EJB):

@Stateless
public class PersonService {

    @PersistenceContext
    private EntityManager em;

    public void create(Person person) {
        em.persist(person);
    }

    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM