Facelet 不传递来自 <h:inputText> 的信息
Facelet does not pass the information from <h:inputText>
上下文:
实体的字段 "email" 和 "password" 设置为不可为空。然而我得到了 "Column can not be null error"。似乎 Facelet 没有传递来自 h:inputText 字段的值,我不知道为什么。
-
如果字段设置为可为空,则它们将包含空值。
- 如果我手动(在控制器内)将字段设置为测试值
然后将它们相应地保存到数据库中。
- 该代码适用于 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>
注册控制器:
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
没错。
我补充说,编译器看不到注释 javax.enterprise.context.RequestScoped 是因为您的库路径。您可能希望使用 Java EE 6,默认情况下 GlassFish 4.1(Netbeans 8.0 附带的版本)默认仅适用于 Java EE 7。因此,您只需将库 Java EE 6 添加到您的项目并将您的项目重新部署到您的服务器。
上下文:
实体的字段 "email" 和 "password" 设置为不可为空。然而我得到了 "Column can not be null error"。似乎 Facelet 没有传递来自 h:inputText 字段的值,我不知道为什么。
- 如果字段设置为可为空,则它们将包含空值。
- 如果我手动(在控制器内)将字段设置为测试值 然后将它们相应地保存到数据库中。
- 该代码适用于 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>
注册控制器:
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
没错。
我补充说,编译器看不到注释 javax.enterprise.context.RequestScoped 是因为您的库路径。您可能希望使用 Java EE 6,默认情况下 GlassFish 4.1(Netbeans 8.0 附带的版本)默认仅适用于 Java EE 7。因此,您只需将库 Java EE 6 添加到您的项目并将您的项目重新部署到您的服务器。