Search code examples
javamysqljpanetbeansfacelets

Facelet does not pass the information from <h:inputText>


Context:

The field "email" and "password" of an Entity are set to not nullable. Yet I get a "Column can not be null error". It seems that the Facelet is not passing the value from the h:inputText-field and I don't know why.

  • If the fields are set to nullable then they are persisted containing null values.
  • If I set the fields manually (within the controller) to a test-value then they are persisted accordingly to the database.
  • The code works with Eclipse

NOTE: I work with NetBeans 8.0.2, MySQL 5.5, GlassFish 4.1 and EclipseLink as JPA.

QUESTION: Is there are reason why this does not work with NetBeans ? Am I missing something ?

Facelet:

<ui:composition template="/template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">

<ui:define name="content">
    <h:form
      id="registerForm"
      prependId="false">

    <h:message
        for="registerForm"
        showSummary="true"/>

    <h:panelGrid columns="3">

        <f:facet name="header">
            <h:outputText value="#{msg['register']}"/>
        </f:facet>

            <h:outputLabel
                value="#{msg['email']}:"/>
            <h:inputText
                id="email"
                value="#{registerController.customer.email}">
            </h:inputText>
            <h:message for="email"/>

            <h:outputLabel
                value="#{msg['password']}:"/>
            <h:inputSecret
                id="password"
                value="#{registerController.customer.password}">
            </h:inputSecret>
            <h:message for="password"/>

            <h:commandButton
                id="register"
                action="#{registerController.persist()}"
                value="#{msg['register']}" />
            <h:message for="register"/>
    </h:panelGrid>
    </h:form>
</ui:define>
</ui:composition>

RegisterController:

package de.java2enterprise.onlineshop;

import java.io.Serializable;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.transaction.UserTransaction;

import de.java2enterprise.onlineshop.model.Customer;
import javax.faces.bean.RequestScoped;

@Named
@RequestScoped
public class RegisterController implements Serializable{

    private static final long serialVersionUID = 1L;

    @PersistenceUnit
    EntityManagerFactory emf;

    @Resource
    private UserTransaction ut;

    @Inject
    private Customer customer;

    public String persist()
    {
        EntityManager eManager = emf.createEntityManager();

        try 
        {
            //this.custumer.setEmail("test"); // this works fine
            ut.begin();
            eManager.joinTransaction();
            eManager.persist(customer);
            ut.commit();

            FacesMessage message = new FacesMessage("Successfully registered!", "Your ID: " + customer.getIdCUSTOMER());
            FacesContext.getCurrentInstance().addMessage("registerForm", message);

        } catch (Exception e) 
        {
            e.printStackTrace();
            FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_WARN, e.getMessage(), e.getCause().getMessage());
            FacesContext.getCurrentInstance().addMessage("registerForm", m);

        }
        return"/register.xhtml";
    }

    public Customer getCustomer() {return customer;}

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

Solution

  • As you are using CDI with the @Named annotation, you should import

    javax.enterprise.context.RequestScoped

    instead of

    javax.faces.bean.RequestScoped