I am following a book about JEE and trying to implement a simple JSF example. I want to register and persist a Customer via a JSF page and a controller.
The problem is that the attributes ot the injected Customer in the controller are always null, no matter what I type into the JSF registration form.
The registerController.persist() method is getting executed on form submission, though.
When I submit the form with the email "user@email.com" for example, I would expect the injected Customer in the controller to have an email, but it is always null. Perhaps somebody can give me a suggestion where the problem lies.
The JSF page:
<?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>
The Controller:
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";
}
}
And the Customer:
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;
}
}
You are mixing JSF
and CDI
annotations, using javax.inject.Named
(CDI) and javax.faces.bean.RequestScoped
(JSF).
Use javax.enterprise.context.RequestScoped
instead.