Facelet不传递来自<h:inputtext> </h:inputtext>的信息

时间:2015-02-14 17:15:53

标签: java mysql jpa netbeans facelets

上下文

字段&#34;电子邮件&#34;和#34;密码&#34;实体的设置不可为空。然而,我得到一个&#34;列不能为空错误&#34;。似乎Facelet没有从h:inputText-field传递值,我也不知道为什么。

  • 如果字段设置为可为空,则它们将保持为包含空值。
  • 如果我手动(在控制器内)将字段设置为测试值 然后它们会相应地保存到数据库中。
  • 代码适用于Eclipse

注意:我使用NetBeans 8.0.2,MySQL 5.5,GlassFish 4.1和EclipseLink作为JPA。

问题:是否有理由说这不适用于NetBeans?我错过了什么吗?

的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;}
}

2 个答案:

答案 0 :(得分:1)

当您使用带有@Named注释的CDI时,您应该导入

javax.enterprise.context.RequestScoped

而不是

javax.faces.bean.RequestScoped

答案 1 :(得分:0)

这是正确的。

我补充一点,编译器没有看到注释javax.enterprise.context.RequestScoped的事实是由于你的库路径。您可能希望使用Java EE 6,默认情况下,GlassFish 4.1(Netbeans 8.0附带的版本)仅适用于Java EE 7。因此,您只需将库Java EE 6添加到项目中,然后将项目重新部署到服务器。