- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 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/
我已经找到了一些关于这个异常的信息,但我的很奇怪。 Caused by: org.hibernate.PropertyNotFoundException: Could not find a sette
我正在研究 Spring。我无法在 JSP 中显示列表项。它说:在 java.lang.String 类型上找不到属性。我有一个 POJO 类 Student: public class Studen
嗨,我是 hibernate 新手,我使用以下代码并收到以下错误 public class OperProfile { private String empId; private long age;
谁能帮我解决我遇到的异常(exception)情况。 在 servlet 中我将国家列表发送到 jsp request.setAttribute("countries", allCountryList
我在从 tomcat 6 升级到 tomcat 7 后遇到 PropertyNotFoundException。这是我收到的消息: value="#{someHandler.result.someLi
我们有一个自定义的 JSF 组件,它以特定方式呈现一些按钮和一个选择框。这是由扩展 UIInput 的类完成的。 xhtml 模板中使用的自定义组件如下所示: 现在,由于我们从运行 JSF 2.0
我有一个复合组件: 以及调用该复合组件的页面: 和一个包含 Action 的 bean: @Named(value="saveDecision
尝试像#{bean.entity.property}这样引用EL中的托管bean时,有时会抛出javax.el.PropertyNotFoundException: Target Unreachabl
我有一个父类(super class) Person : public class Person { public abstract Type getType(); } 我有它的 2 个子类:
当尝试像#{bean.entity.property}这样在EL中引用托管bean时,有时会抛出javax.el.PropertyNotFoundException: Target Unreachab
当我尝试渲染 View 时,浏览器显示此错误 01:46:11,371 GRAVE [javax.enterprise.resource.webcontainer.jsf.application] (
尝试像#{bean.entity.property}这样引用EL中的托管bean时,有时会抛出javax.el.PropertyNotFoundException: Target Unreachabl
我尝试了 omniFaces 的 AjaxExceptionHandler(非常有用)并将其配置为捕获 java.lang.Throwable以及源自各种请求(不仅是 ajax 请求)的异常。 它工作
当试图像#{bean.entity.property}这样在EL中引用托管bean时,有时会抛出javax.el.PropertyNotFoundException: Target Unreachab
我想从数据库中检索整个表并将其显示到我的 jsp 页面中,但出现错误 我的 StaffBean。 java是 package com.staff.bean; import java.util.Date
我正在开发一个小型消息传递 Web 应用程序来学习 jsp 和 servlet。我有一个具有以下属性的 MessageModel 类: String toAddress ; String fromAd
当尝试像#{bean.entity.property}这样引用EL中的托管bean时,有时会抛出javax.el.PropertyNotFoundException: Target Unreachab
我正在尝试让 Fluent NHibernate 为我映射一个集合。我的类定义如下: public abstract class Team { public virtual Guid Id {
我在 JSP 中遇到错误,我无法弄清楚是什么原因导致的。我已经包含了所有适当的库,并且我确保遵循大写/小写的 bean 约定。这是 JSP 中的相关代码: ${entry.price}
我有两个 POJO,STOCK 和 STOCK_DETAILS(一对多关系)。我还有一个接口(interface) IAUDITLOG(有两种方法)。我需要用两个 POJO 来实现这个接口(inter
我是一名优秀的程序员,十分优秀!