繁体   English   中英

JSF + Hibernate serverError:类javax.el.PropertyNotFoundException

[英]JSF + Hibernate serverError: class javax.el.PropertyNotFoundException

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

我得到该例外的原因是什么?

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

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

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

您好,我了解您的问题,但我遇到了同样的错误,然后才解决。 问题是索引中的customer.firstName没有目标(Ctrl +单击index.xhml中的customer.firstName)您没有目标。

这个Customer.java文件是一个Java类文件,但是根据JSF,我们需要创建Managed bean文件(名称: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.

而已。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM