2017-06-23 6 views
0

JEEについての書籍を読んで、簡単なJSFの例を実装しようとしています。私は顧客をJSFページとコントローラを介して登録して永続化したいと思います。注入されたBeanのフィールドは、JSFを介して設定しようとしたときにnullです。

問題は、コントローラの注入顧客の属性は、JSF登録フォームに何を入力していても常にnullです。

フォーム提出でregisterController.persist()メソッドが実行されていますが、

たとえば、電子メール "[email protected]"でフォームを送信すると、コントローラの注入された顧客は電子メールを受け取ると予想されますが、常にnullです。おそらく、誰かが私に問題のある場所の提案を与えることができます。

JSFページ:

<?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:h="http://xmlns.jcp.org/jsf/html" 
     xmlns:ui="http://xmlns.jcp.org/jsf/facelets" 
     xmlns:f="http://xmlns.jcp.org/jsf/core"> 
<h:head> 
    <title> 
     <h:outputText value="Onlineshop"/> 
    </title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
</h:head> 
<h:body> 
    <h:form> 
     <h:panelGrid columns="2"> 
      <f:facet name="header"> 
       <h:outputText value="Registrieren"/> 
      </f:facet> 
      <h:outputLabel value="E-Mail:"/> 
      <h:inputText value="#{registerController.customer.email}"></h:inputText> 
      <h:outputLabel value="Kennwort:"/> 
      <h:inputSecret value="#{registerController.customer.password}"></h:inputSecret> 
      <h:commandButton action="#{registerController.persist}" value="Registrieren"/> 
     </h:panelGrid> 
    </h:form> 
</h:body> 
</html> 

コントローラー:

package de.java2enterprise.onlineshop; 

import de.java2enterprise.onlineshop.model.Customer; 

import javax.annotation.Resource; 
import javax.faces.bean.RequestScoped; 
import javax.inject.Inject; 
import javax.inject.Named; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.Persistence; 
import javax.persistence.PersistenceUnit; 
import javax.transaction.UserTransaction; 

@Named 
@RequestScoped 
public class RegisterController { 

    @PersistenceUnit() 
    private EntityManagerFactory emf; 

    @Resource 
    private UserTransaction ut; 

    @Inject 
    private Customer customer; 

    public Customer getCustomer() { 
     return customer; 
    } 

    public void setCustomer(Customer customer) { 
     this.customer = customer; 
    } 

    public String persist() { 
     try { 
      ut.begin(); 
      emf.createEntityManager().persist(customer); 
      ut.commit(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return "/register.xhtml"; 
    } 
} 

と顧客:

package de.java2enterprise.onlineshop.model; 

import java.io.Serializable; 
import java.util.HashSet; 
import java.util.Set; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.NamedQuery; 
import javax.persistence.OneToMany; 
import javax.persistence.SequenceGenerator; 
import javax.persistence.Table; 

/** 
* 
* @author Alexander Salvanos 
* 
* The persistent class for the CUSTOMER database table. 
* 
*/ 
@Entity 
@Table(schema="ONLINESHOP", name="CUSTOMER") 
@NamedQuery(
     name="Customer.findAll", 
     query="SELECT c FROM Customer c") 
public class Customer implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @SequenceGenerator(
      name="CUSTOMER_ID_GENERATOR", 
      sequenceName="SEQ_CUSTOMER", 
      schema="ONLINESHOP", 
      allocationSize=1, 
      initialValue=1) 
    @GeneratedValue(
      strategy=GenerationType.SEQUENCE, 
      generator="CUSTOMER_ID_GENERATOR") 
    private Long id; 

    private String email; 

    private String password; 

    //bi-directional many-to-one association to Item 
    @OneToMany(mappedBy="seller") 
    private Set<Item> offers; 

    //bi-directional many-to-one association to Item 
    @OneToMany(mappedBy="buyer") 
    private Set<Item> purchases; 

    public Customer() { 
    } 

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

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getEmail() { 
     return this.email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getPassword() { 
     return this.password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public Set<Item> getOffers() { 
     return this.offers; 
    } 

    public void setOffers(Set<Item> offers) { 
     this.offers = offers; 
    } 

    public Item addOffer(Item offer) { 
     Set<Item> offers = getOffers(); 
     if(offers == null) { 
      offers = new HashSet<Item>(); 
     } 
     offers.add(offer); 
     offer.setSeller(this); 

     return offer; 
    } 

    public Item removeOffer(Item offer) { 
     getOffers().remove(offer); 
     offer.setSeller(null); 

     return offer; 
    } 

    public Set<Item> getPurchases() { 
     return this.purchases; 
    } 

    public void setPurchases(Set<Item> purchases) { 
     this.purchases = purchases; 
    } 

    public Item addPurchase(Item purchase) { 
     Set<Item> purchases = getPurchases(); 
     if(purchases == null) { 
      purchases = new HashSet<Item>(); 
     } 
     purchases.add(purchase); 
     purchase.setBuyer(this); 
     return purchase; 
    } 

    public Item removePurchase(Item purchase) { 
     getPurchases().remove(purchase); 
     purchase.setBuyer(null); 

     return purchase; 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((id == null) ? 0 : id.hashCode()); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) { 
      return true; 
     } 
     if (obj == null) { 
      return false; 
     } 
     if (!(obj instanceof Customer)) { 
      return false; 
     } 
     Customer other = (Customer) obj; 
     if (id == null) { 
      if (other.id != null) { 
       return false; 
      } 
     } else if (!id.equals(other.id)) { 
      return false; 
     } 
     return true; 
    } 

    public String toString() { 
     return id + "-" + email + "-" + password; 
    } 
} 
+0

エンティティには値がありますか?またはそれは 'ヌル'ですか? –

+0

値はありますが、フィールドはすべてnullです。 – Florian

+1

javax.faces.bean.RequestScopedの代わりに 'javax.enterprise.context.RequestScoped'パッケージを使用してください。問題ではないかもしれませんが、JSFとCDIの注釈を混同しないでください。 – Rouliboy

答えて

0

あなたは、JSFCDI注釈を混合javax.inject.Named(CDI)を使用していますjavax.faces.bean.RequestScoped(JSF)。

javax.enterprise.context.RequestScopedを代わりに使用してください。

関連する問題