我使用JSF,Hibernate创建了一个非常简单的CRUD应用程序,并将它们与MySQL集成在一起。问题是,每当我尝试添加新数据时,我都会遇到以下异常:
serverError: class javax.el.PropertyNotFoundException Target Unreachable, identifier 'customer' resolved to null
以下是我的观点:
<h:head>
<title>JSF Hibernate CRUD Example</title>
</h:head>
<h:body>
<h:form>
<h:panelGrid id="panel1" columns="2" border="1"
cellpadding="5" cellspacing="1">
<f:facet name="header">
<h:outputText value="Add Customer Information"/>
</f:facet>
<h:outputLabel value="First Namer:"/>
<h:inputText value="#{customer.firstName}" id="fn"/>
<h:outputLabel value="Last Name:"/>
<h:inputText value="#{customer.lastName}" id="ln"/>
<h:outputLabel value="Email:"/>
<h:inputText value="#{customer.email}" id="eml"/>
<h:outputLabel value="Date of Birth:"/>
<h:inputText value="#{customer.sd}" id="s"/>
<f:facet name="footer">
<h:outputLabel value="#{customer.msg}" id="msg" styleClass="msg"/>
<h:commandButton value="Save" action="#{customer.saveCustomer}">
<f:ajax render="fn ln eml s msg" execute="@form"/>
</h:commandButton>
</f:facet>
</h:panelGrid>
</h:form>
<h:form>
<h:panelGrid id="panel2" columns="2" border="1"
cellpadding="5" cellspacing="1">
<f:facet name="header">
<h:outputText value="Update/Delete Customer Info"/>
</f:facet>
<h:outputLabel value="Select Customer:"/>
<h:selectOneMenu value="#{customer.selectedname}" id="ulist">
<f:selectItems value="#{customer.allCustomers}"/>
<f:ajax event="change" render="cid fname lname email sd" listener="#{customer.fullInfo}"/>
</h:selectOneMenu>
<h:outputLabel value="Customer ID:"/>
<h:inputText value="#{customer.custId}" id="cid" readonly="true"/>
<h:outputLabel value="First Name:"/>
<h:inputText value="#{customer.firstName}" id="fname"/>
<h:outputLabel value="Last Name:"/>
<h:inputText value="#{customer.lastName}" id="lname"/>
<h:outputLabel value="Email:"/>
<h:inputText value="#{customer.email}" id="email"/>
<h:outputLabel value="Date of Birth:"/>
<h:inputText value="#{customer.sd}" id="sd"/>
<f:facet name="footer">
<h:outputLabel value="#{customer.msg}" id="msg2" styleClass="msg"/>
<h:commandButton value="Update Info" action="#{customer.updateCustomer}">
<f:ajax render="ulist cid fname lname email sd msg2" execute="@form"/>
</h:commandButton>
<h:commandButton value="Delete Info" action="#{customer.deleteCustomer}">
<f:ajax render="ulist cid fname lname email sd msg2" execute="@form"/>
</h:commandButton>
</f:facet>
</h:panelGrid>
</h:form>
</h:body>
这是我的实体类 - Customer.java
:
package com.javaknowledge.entity;
import com.javaknowledge.dao.CustomerDao;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
/**
*
* @author Capt. Jack Sparrow, Pirate Lord of the Caribbean
*/
public class Customer implements java.io.Serializable {
private Integer custId;
private String firstName;
private String lastName;
private String email;
private Date dob;
private String sd, msg, selectedname;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public Customer() {
}
public Customer(String firstName, String lastName, String email, Date dob) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.dob = dob;
}
public String getSd() {
return sd;
}
public void setSd(String sd) {
this.sd = sd;
}
public Integer getCustId() {
return this.custId;
}
public void setCustId(Integer custId) {
this.custId = custId;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getDob() {
return this.dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getSelectedname() {
return selectedname;
}
public void setSelectedname(String selectedname) {
this.selectedname = selectedname;
}
public void saveCustomer() {
try {
Date d = sdf.parse(sd);
System.out.println(d);
this.dob = d;
} catch (ParseException e) {
e.printStackTrace();
}
CustomerDao dao = new CustomerDao();
dao.addCustomer(this);
this.msg = "Member Info Saved Successfull!";
clearAll();
}
public void updateCustomer() {
try {
Date d = sdf.parse(sd);
System.out.println(d);
this.dob = d;
} catch (ParseException e) {
e.printStackTrace();
}
CustomerDao dao = new CustomerDao();
dao.updateCustomer(this);
this.msg = "Member Info Update Successfull!";
clearAll();
}
public void deleteCustomer() {
CustomerDao dao = new CustomerDao();
dao.deleteCustomer(custId);
this.msg = "Member Info Delete Successfull!";
clearAll();
}
public List<Customer> getAllCustomers() {
List<Customer> users = new ArrayList<Customer>();
CustomerDao dao = new CustomerDao();
users = dao.getAllCustomers();
return users;
}
public void fullInfo() {
CustomerDao dao = new CustomerDao();
List<Customer> lc = dao.getCustomerById(selectedname);
System.out.println(lc.get(0).firstName);
this.custId = lc.get(0).custId;
this.firstName = lc.get(0).firstName;
this.lastName = lc.get(0).lastName;
this.email = lc.get(0).email;
this.dob = lc.get(0).dob;
this.sd = sdf.format(dob);
}
private void clearAll() {
this.firstName = "";
this.lastName = "";
this.sd = "";
this.email = "";
this.custId = 0;
}
}
我在DAO类中的 addCustomer ()方法:
public void addCustomer(Customer cust) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
session.save(cust);
session.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
}
我得到这个例外的原因是什么?
P.S。我是JSF和Hibernate的初学者。
编辑 - 1:
此代码后来生成:
package javax.faces.bean;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.TYPE})
@Inherited
public @interface ManagedBean {
public String name() default "";
public boolean eager() default false;
}
答案 0 :(得分:1)
由于您的Customer实体是hibernate映射实体,因此不要尝试将其添加到spring容器或通过somr以其他方式使其成为托管bean。您应该创建另一个名为CustomerBackingBean的类,并将customer定义为字段。还有一些方法可以使用你的dao从db加载客户。加载后,您将能够在页面上看到它。
你应该试着自己动手去完全理解它是如何工作的:)。 祝你好运。
答案 1 :(得分:-1)
嗨我理解你的问题我得到了同样的错误然后我解决了它。 问题是index中的customer.firstName没有目标(ctrl +点击index.xhml中的customer.firstName)你没有目标。
这个Customer.java文件是一个java类文件,但根据JSF我们需要创建Managed bean文件(name:Customer.java)。如果你不知道如何按照我的步骤创建它(在Customer.java中首先复制代码然后删除该文件)
step 1: Right-click on the com.knowledge.entity source package node and choose New > Other.
Step 2: Select JSF Managed Bean from the JavaServer Faces category. Click Next.
Step 3: Type Customer for the Class Name.
Step 4: You will use the Managed Bean name Customer as the value for the inputText and commandButton in the JSF page index.xhtml when calling methods in the bean.
Step 5: Select com.javaknowledge.entity for the Package.
Step 6: Type customer for the Name that will be used for the managed bean.
Step 7: Set Scope to Session. Click Finish.
是的。