This question already has an answer here:
JSF does not populate @Named @RequestScoped bean with submitted input values
                                
                                    (1个答案)
                                
                        
                                2年前关闭。
            
                    
内容:

实体的“电子邮件”和“密码”字段设置为不可为空。但是我得到了“列不能为空错误”。看来Facelet没有从h:inputText-field传递值,我也不知道为什么。



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


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

问题:是否有原因为什么不适用于NetBeans?我想念什么吗?

小面:

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

最佳答案

在使用带有@Named批注的CDI时,应导入

javax.enterprise.context.RequestScoped

代替

javax.faces.bean.RequestScoped

07-28 02:16