1

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

现在,这仅在我通过#{customerRegistration.newCustomer}. 当我简单地使用时#{newCustomer},姓氏的输出是null每当我提交表单时。

这里发生了什么?这是根据第 7.1 章限制JSR-299 规范的 bean 实例的预期行为吗?

它说:

...但是,如果应用程序直接实例化一个 bean 类,而不是让容器执行实例化,则生成的实例不由容器管理,也不是第 6.5.2 节定义的上下文实例,“a豆”。此外,第 2.1 节“容器为 bean 提供的功能”中列出的功能将不适用于该特定实例。在已部署的应用程序中,容器负责实例化 bean 并初始化它们的依赖项。...

这是代码:

客户.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();
    }
}

客户注册.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>

客户转换器.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();
    }

}
4

1 回答 1

3

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

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

这是预期的行为。

似乎您想要的东西可以在Customer每个会话中为您提供一个新实体。在这种情况下,正确的方法是:

public class CustomerProducer {

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

}

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

于 2012-11-03T14:09:56.420 回答