引用 CDI 生成器方法导致 h:selectOneMenu

发布于 2024-12-05 09:22:53 字数 6529 浏览 0 评论 0原文

我有一个命名会话作用域 bean CustomerRegistration,它有一个命名生产者方法 getNewCustomer,它返回一个 Customer 对象。还有 CustomerListProducer 类,它从数据库中生成所有客户的列表。在 selectCustomer.xhtml 页面上,用户可以选择其中一位客户并将选择提交到应用程序,然后应用程序只需打印出所选客户的姓氏。

现在,仅当我通过 #{customerRegistration.newCustomer} 在 Facelets 页面上引用所选客户时,此功能才有效。当我简单地使用 #{newCustomer} 时,每当我提交表单时,姓氏的输出都是 null

这是怎么回事?这是 JSR-299 规范第 7.1 章“bean 实例的限制”中的预期行为吗?

它说:

...但是,如果应用程序直接实例化一个bean类, 而不是让容器执行实例化,结果是 实例不受容器管理,也不是上下文 实例如第 6.5.2 节“bean 的上下文实例”中定义。 此外,第 2.1 节“功能”中列出的功能 由容器提供给 bean”将无法使用 特定实例。在已部署的应用程序中,它是容器 负责实例化 Bean 并初始化它们 依赖关系。 ...

这是代码:

Customer.java:

@javax.persistence.Entity
@Veto
public class Customer implements Serializable, Entity {
    private static final long serialVersionUID = 122193054725297662L;
    @Column(name = "first_name")
    private String firstName;
    @Column(name = "last_name")
    private String lastName;
    @Id
    @GeneratedValue()
    private Long id;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return firstName + ", " + lastName;
    }

    @Override
    public Long getId() {
        return this.id;
    }
}

CustomerListProducer.java:

@SessionScoped
public class CustomerListProducer implements Serializable {

    @Inject
    private EntityManager em;

    private List<Customer> customers;

    @Inject
    @Category("helloworld_as7")
    Logger log;

    // @Named provides access the return value via the EL variable name
    // "members" in the UI (e.g.,
    // Facelets or JSP view)
    @Produces
    @Named
    public List<Customer> getCustomers() {
        return customers;
    }

    public void onCustomerListChanged(
            @Observes(notifyObserver = Reception.IF_EXISTS) final Customer customer) {
//      retrieveAllCustomersOrderedByName();
        log.info(customer.toString());
    }

    @PostConstruct
    public void retrieveAllCustomersOrderedByName() {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Customer> criteria = cb.createQuery(Customer.class);
        Root<Customer> customer = criteria.from(Customer.class);
        // Swap criteria statements if you would like to try out type-safe
        // criteria queries, a new
        // feature in JPA 2.0
        // criteria.select(member).orderBy(cb.asc(member.get(Member_.name)));
        criteria.select(customer).orderBy(cb.asc(customer.get("lastName")));
        customers = em.createQuery(criteria).getResultList();
    }
}

CustomerRegistration.java:

@Named
@SessionScoped
public class CustomerRegistration implements Serializable {

    @Inject
    @Category("helloworld_as7")
    private Logger log;

    private Customer newCustomer;

    @Produces
    @Named
    public Customer getNewCustomer() {
        return newCustomer;
    }

    public void selected() {
        log.info("Customer " + newCustomer.getLastName() + " ausgewählt.");
    }

    @PostConstruct
    public void initNewCustomer() {
        newCustomer = new Customer();
    }

    public void setNewCustomer(Customer newCustomer) {
        this.newCustomer = newCustomer;
    }

}

不工作 selectCustomer.xhtml:

<?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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <title>Auswahl</title>
</h:head>
<h:body>
    <h:form>
        <h:selectOneMenu value="#{newCustomer}" converter="customerConverter">
            <f:selectItems value="#{customers}" var="current"
                itemLabel="#{current.firstName}, #{current.lastName}" />
        </h:selectOneMenu>
        <h:panelGroup id="auswahl">
            <h:outputText value="#{newCustomer.lastName}" />
        </h:panelGroup>
        <h:commandButton value="Klick"
            action="#{customerRegistration.selected}" />
    </h:form>
</h:body>
</html>

工作 selectCustomer.xhtml:

<?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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <title>Auswahl</title>
</h:head>
<h:body>
    <h:form>
        <h:selectOneMenu value="#{customerRegistration.newCustomer}" converter="customerConverter">
            <f:selectItems value="#{customers}" var="current"
                itemLabel="#{current.firstName}, #{current.lastName}" />
        </h:selectOneMenu>
        <h:panelGroup id="auswahl">
            <h:outputText value="#{newCustomer.lastName}" />
        </h:panelGroup>
        <h:commandButton value="Klick"
            action="#{customerRegistration.selected}" />
    </h:form>
</h:body>
</html>

CustomerConverter.java:

@SessionScoped
@FacesConverter("customerConverter")
public class CustomerConverter implements Converter, Serializable {
    private static final long serialVersionUID = -6093400626095413322L;

    @Inject
    EntityManager entityManager;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component,
            String value) {
        Long id = Long.valueOf(value);
        return entityManager.find(Customer.class, id);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component,
            Object value) {
        return ((Customer) value).getId().toString();
    }

}

I have a named session scoped bean CustomerRegistration which has a named producer method getNewCustomer which returns a Customer object. There is also CustomerListProducer class which produces all customers as list from the database. On the selectCustomer.xhtml page the user is then able to select one of the customers and submit the selection to the application which then simply prints out the last name of the selected customer.

Now this only works when I reference the selected customer on the facelets page via #{customerRegistration.newCustomer}. When I simply use #{newCustomer} then the output for the last name is null whenever I submit the form.

What's going on here? Is this the expected behavior as according to chapter 7.1 Restriction upon bean instantion of JSR-299 spec?

It says:

... However, if the application directly instantiates a bean class,
instead of letting the container perform instantiation, the resulting
instance is not managed by the container and is not a contextual
instance as defined by Section 6.5.2, “Contextual instance of a bean”.
Furthermore, the capabilities listed in Section 2.1, “Functionality
provided by the container to the bean” will not be available to that
particular instance. In a deployed application, it is the container
that is responsible for instantiating beans and initializing their
dependencies. ...

Here's the code:

Customer.java:

@javax.persistence.Entity
@Veto
public class Customer implements Serializable, Entity {
    private static final long serialVersionUID = 122193054725297662L;
    @Column(name = "first_name")
    private String firstName;
    @Column(name = "last_name")
    private String lastName;
    @Id
    @GeneratedValue()
    private Long id;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return firstName + ", " + lastName;
    }

    @Override
    public Long getId() {
        return this.id;
    }
}

CustomerListProducer.java:

@SessionScoped
public class CustomerListProducer implements Serializable {

    @Inject
    private EntityManager em;

    private List<Customer> customers;

    @Inject
    @Category("helloworld_as7")
    Logger log;

    // @Named provides access the return value via the EL variable name
    // "members" in the UI (e.g.,
    // Facelets or JSP view)
    @Produces
    @Named
    public List<Customer> getCustomers() {
        return customers;
    }

    public void onCustomerListChanged(
            @Observes(notifyObserver = Reception.IF_EXISTS) final Customer customer) {
//      retrieveAllCustomersOrderedByName();
        log.info(customer.toString());
    }

    @PostConstruct
    public void retrieveAllCustomersOrderedByName() {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Customer> criteria = cb.createQuery(Customer.class);
        Root<Customer> customer = criteria.from(Customer.class);
        // Swap criteria statements if you would like to try out type-safe
        // criteria queries, a new
        // feature in JPA 2.0
        // criteria.select(member).orderBy(cb.asc(member.get(Member_.name)));
        criteria.select(customer).orderBy(cb.asc(customer.get("lastName")));
        customers = em.createQuery(criteria).getResultList();
    }
}

CustomerRegistration.java:

@Named
@SessionScoped
public class CustomerRegistration implements Serializable {

    @Inject
    @Category("helloworld_as7")
    private Logger log;

    private Customer newCustomer;

    @Produces
    @Named
    public Customer getNewCustomer() {
        return newCustomer;
    }

    public void selected() {
        log.info("Customer " + newCustomer.getLastName() + " ausgewählt.");
    }

    @PostConstruct
    public void initNewCustomer() {
        newCustomer = new Customer();
    }

    public void setNewCustomer(Customer newCustomer) {
        this.newCustomer = newCustomer;
    }

}

not working selectCustomer.xhtml:

<?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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <title>Auswahl</title>
</h:head>
<h:body>
    <h:form>
        <h:selectOneMenu value="#{newCustomer}" converter="customerConverter">
            <f:selectItems value="#{customers}" var="current"
                itemLabel="#{current.firstName}, #{current.lastName}" />
        </h:selectOneMenu>
        <h:panelGroup id="auswahl">
            <h:outputText value="#{newCustomer.lastName}" />
        </h:panelGroup>
        <h:commandButton value="Klick"
            action="#{customerRegistration.selected}" />
    </h:form>
</h:body>
</html>

working selectCustomer.xhtml:

<?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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <title>Auswahl</title>
</h:head>
<h:body>
    <h:form>
        <h:selectOneMenu value="#{customerRegistration.newCustomer}" converter="customerConverter">
            <f:selectItems value="#{customers}" var="current"
                itemLabel="#{current.firstName}, #{current.lastName}" />
        </h:selectOneMenu>
        <h:panelGroup id="auswahl">
            <h:outputText value="#{newCustomer.lastName}" />
        </h:panelGroup>
        <h:commandButton value="Klick"
            action="#{customerRegistration.selected}" />
    </h:form>
</h:body>
</html>

CustomerConverter.java:

@SessionScoped
@FacesConverter("customerConverter")
public class CustomerConverter implements Converter, Serializable {
    private static final long serialVersionUID = -6093400626095413322L;

    @Inject
    EntityManager entityManager;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component,
            String value) {
        Long id = Long.valueOf(value);
        return entityManager.find(Customer.class, id);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component,
            Object value) {
        return ((Customer) value).getId().toString();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那请放手 2024-12-12 09:22:53

想想@Producer 是如何注册的。在部署期间,容器会扫描您的类以获取注释,并且您在 @SessionScoped @Named bean 中声明了 @Producer 方法这一事实并不意味着您将拥有与该 bean 实例一样多的生产者,也不意味着容器将从您期望调用它的实例中调用生产者。

这里发生的情况是 - 您的生产者方法始终返回相同的 Customer 实例,该实例是在部署期间(即 @Producer)期间在您的 @PostConstruct 方法中创建的实例注册。

这是预期的行为。

看来您想要为每个会话提供一个新的 Customer 实体。在这种情况下,正确的方法是:

public class CustomerProducer {

    @Produces @Named @SessionScoped
    public Customer getNewCustomer(@New Customer customer) {
        // do some custom init if need be
        return customer;
    }

}

然后从会话范围 bean 中删除 @Producer 相关注释。现在您可以使用“#{newCustomer}”,它将始终为每个会话提供一个新的容器托管实例。

Think of how @Producers are registered. During deployment time, the container scans your classes for annotations and the fact that you have your @Producer method declared inside a @SessionScoped @Named bean, doesn't mean that you will have as many producers as instances of that bean, nor does it mean that the container will call the producer from the instance you expect it to be called.

What happens here is - your producer method always returns the same instance of Customer, the one that was created in your @PostConstruct method during deployment, i.e. during @Producer registration.

This is the expected behavior.

It seems that you want something that will give you a new Customer entity per session. In that case, the correct way to do this would be:

public class CustomerProducer {

    @Produces @Named @SessionScoped
    public Customer getNewCustomer(@New Customer customer) {
        // do some custom init if need be
        return customer;
    }

}

Then remove @Producer related annotations from you session scoped bean. Now you can use `#{newCustomer}, which will always give you a new container managed instance per session.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文