オブジェクトを返すCustomerRegistration
名前付きプロデューサーメソッドを持つ名前付きセッションスコープのBeanがあります。データベースからすべての顧客をリストとして作成するクラスもあります。selectCustomer.xhtmlページで、ユーザーは顧客の1つを選択し、その選択をアプリケーションに送信できます。アプリケーションは、選択した顧客の名前を印刷するだけです。getNewCustomer
Customer
CustomerListProducer
これは、を介してファセットページで選択した顧客を参照する場合にのみ機能します#{customerRegistration.newCustomer}
。単純に使用すると、フォームを送信するたびに#{newCustomer}
姓の出力が返されます。null
何が起きてる?これは、7.1章JSR-299仕様のBeanインスタンス化の制限によると予想される動作ですか?
それは言う:
...ただし、アプリケーションがBeanクラスを直接インスタンス化する場合、コンテナにインスタンス化を実行させるのではなく、結果のインスタンスはコンテナによって管理されず、6.5.2項「豆"。さらに、セクション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();
}
}