gpt4 book ai didi

java - JSF + Hibernate 服务器错误 : class javax. el.PropertyNotFoundException

转载 作者:行者123 更新时间:2023-11-29 23:19:17 24 4
gpt4 key购买 nike

我使用 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();
}
}

我收到该异常的原因是什么?

附注我是 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;
}

最佳答案

由于您的 Customer 实体是 hibernate 映射实体,因此不要尝试将其添加到 spring 容器或通过 somr 其他方式使其托管 bean。您应该创建另一个名为 CustomerBackingBean 之类的类,并将 customer 定义为字段。还应该存在一些方法来使用您的 dao 从数据库加载客户。加载后,您将能够在页面上看到它。

您应该尝试自己思考才能完全理解它是如何工作的:)。祝你好运。

关于java - JSF + Hibernate 服务器错误 : class javax. el.PropertyNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27468975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com