PrimeFaces PropertyNotFoundException目标无法访问,'null'返回null

时间:2013-08-06 10:31:38

标签: spring primefaces

我正在创建我正在创建的网站。我是Faces 2.0的新手,只使用JSP,现在转向Faces 2.0。我有一个页面添加和删除邮政编码的位置。编辑部分工作,但添加部分,我有一个错误:

PropertyNotFoundException, Target Unreachable, 'null' returned null.

关注以下值:

value="#{zipCodeMaintennanceBean.selectedZipCode.description}"

我不知道它为什么不起作用,因为编辑部分可以工作,并且它们使用相同的ManagedBean。

我使用Spring 3.1.3使用Hibernate和PrimeFaces 3.5。我希望有人回答我的问题,因为我不知道如何解决这个问题。

我怀疑每当我点击按钮时,正在创建一个新对象,因为我正在使用@RequestScoped注释。

我尝试在@PostConstruct方法上实例化实体类,甚至在声明上实例化实体并在描述上设置空字符串并且没有任何作用。

它不会调用save()方法,似乎卡在更新模型值阶段

以下是我的xhtml页面:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:c="http://java.sun.com/jsp/jstl/core">

<ui:composition template="/WEB-INF/views/template.xhtml">
    <ui:define name="content">
        <table border="0" width="100%">
            <tr align="center">
                <td><c:if test="${not empty param.login_error}">
                        <div class="error"
                            style="font-family: serif; font-size: medium; color: red;">
                            #{msg['label.login.invalidlogon']}<br />
                            #{sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}
                        </div>
                    </c:if> <c:if test="">
                        <div class="message"
                            style="font-family: serif; font-size: medium; color: red;">
                            ${sessionScope.messageVariable}</div>
                    </c:if> &nbsp;</td>
            </tr>
        </table>

        <p:panelGrid columns="3">
            <h:form id="form">
                <f:facet name="header">
                    <p:row>
                        <p:column colspan="3">#{msg['usrmnt.header']}</p:column>
                    </p:row>
                </f:facet>
                <p:row>
                    <p:column>
                        <h:outputLabel for="userSearch" value="Search ZipCode/Description" />
                    </p:column>
                    <p:column>
                        <p:inputText styleClass="new-fld" id="userSearch"
                            value="#{zipCodeMaintennanceBean.searchString}">
                        </p:inputText>
                    </p:column>
                    <p:column>
                        <p:commandButton value="#{msg['usrmnt.searchbutton']}"
                            actionListener="#{zipCodeMaintennanceBean.search}"
                            update="panel,dataTable" process="@form" />
                        <p:commandButton value="Add"
                            actionListener="#{zipCodeMaintennanceBean.triggerAdd}"
                            update="panel,:#{p:component('displayadd')}"
                            oncomplete="addDialog.show()" />
                    </p:column>
                </p:row>

                <p:row>
                    <p:column colspan="3" styleClass="ui-widget-header">
                        <p:spacer height="0" />
                    </p:column>
                </p:row>
                <p:row>
                    <p:column colspan="3">
                        <p:outputPanel id="panel">
                            <p:dataTable id="dataTable" var="zip"
                                value="#{zipCodeMaintennanceBean.zipCodes}" paginator="true"
                                rows="10"
                                paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                                rowsPerPageTemplate="5,10,15"
                                rendered="#{zipCodeMaintennanceBean.hasSearchResult}"
                                selectionMode="single"
                                selection="#{zipCodeMaintennanceBean.selectedZipCode}"
                                rowKey="#{zip.zipcode}">

                                <p:ajax event="rowSelect"
                                    listener="#{zipCodeMaintennanceBean.onRowSelect}"
                                    oncomplete="editDialog.show()"
                                    update=":#{p:component('display')}" />
                                <p:ajax event="rowUnselect"
                                    listener="#{zipCodeMaintennanceBean.onRowUnselect}" />

                                <f:facet name="header">  
                                    Users
                                </f:facet>


                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="Zip Code" />
                                    </f:facet>
                                    <h:outputText value="#{zip.zipcode}" />
                                </p:column>
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="Description" />
                                    </f:facet>
                                    <h:outputText value="#{zip.description}" />
                                </p:column>
                            </p:dataTable>
                        </p:outputPanel>
                    </p:column>
                </p:row>
                <p:dialog header="Add ZipCode" widgetVar="addDialog" id="addDialog"
                    modal="true" showEffect="clip" hideEffect="fold" dynamic="true">
                    <p:outputPanel autoUpdate="true">
                        <p:panelGrid cellpadding="4" id="displayadd"
                            rendered="#{zipCodeMaintennanceBean.addMode}"
                            styleClass="maintennancePanelGrid">
                            <f:facet name="header">
                                <p:row>
                                    <p:column colspan="3">
                                        <h:outputText value="Add Zip Code" />
                                    </p:column>
                                </p:row>
                            </f:facet>
                            <p:row>
                                <p:column>
                                    <h:outputLabel for="zipcodeAdd" value="Zip Code:" />
                                </p:column>
                                <p:column>
                                    <p:inputText styleClass="new-fld" id="zipcodeAdd"
                                        value="#{zipCodeMaintennanceBean.selectedZipCode.zipcode}"
                                        label="Zip Code" required="true">
                                        <f:validateLength for="userName" minimum="4" maximum="6"></f:validateLength>
                                    </p:inputText>
                                </p:column>
                                <p:column>
                                    <p:message for="zipcodeAdd"></p:message>
                                </p:column>
                            </p:row>
                            <p:row>
                                <p:column>
                                    <h:outputLabel for="descriptionAdd" value="Description: " />
                                </p:column>
                                <p:column>
                                    <p:inputText styleClass="new-fld" id="descriptionAdd"
                                        required="true"
                                        value="#{zipCodeMaintennanceBean.selectedZipCode.description}"
                                        label="Description"></p:inputText>

                                </p:column>
                                <p:column>
                                    <p:message for="descriptionAdd"></p:message>
                                </p:column>
                            </p:row>
                            <f:facet name="footer">
                                <p:row>
                                    <p:column styleClass="text-align:center" colspan="3">
                                        <p:commandButton value="Submit"
                                            update=":#{p:component('displayadd')}"
                                            action="#{zipCodeMaintennanceBean.add}"
                                            oncomplete="if (!args.validationFailed) addDialog.hide()" process="@form" />
                                    </p:column>
                                </p:row>
                                <p:row>
                                    <p:column colspan="3">

                                    </p:column>
                                </p:row>
                            </f:facet>
                        </p:panelGrid>
                    </p:outputPanel>
                </p:dialog>
                <p:dialog header="Edit ZipCode" resizable="false" id="editDialog"
                    widgetVar="editDialog" modal="true" showEffect="clip"
                    hideEffect="fold" closeOnEscape="true" dynamic="true">
                    <p:outputPanel autoUpdate="true">
                        <p:panelGrid cellpadding="4" id="display"
                            rendered="#{zipCodeMaintennanceBean.hasSelectedZipCode}"
                            styleClass="maintennancePanelGrid">

                            <f:facet name="header">
                                <p:row>
                                    <p:column colspan="3">
                                        <h:outputText value="Edit Zip Code" />
                                    </p:column>
                                </p:row>

                            </f:facet>
                            <p:row>
                                <p:column>
                                    <h:outputLabel for="zipcode" value="Zip Code:" />
                                </p:column>
                                <p:column>
                                    <p:inputText styleClass="new-fld" id="zipcode"
                                        value="#{zipCodeMaintennanceBean.selectedZipCode.zipcode}"
                                        label="Zip Code" disabled="true">
                                        <f:validateLength for="userName" minimum="4" maximum="6"></f:validateLength>
                                    </p:inputText>
                                </p:column>
                                <p:column>
                                    <p:message for="zipcode"></p:message>
                                </p:column>
                            </p:row>
                            <p:row>
                                <p:column>
                                    <h:outputLabel for="descripton" value="Description: " />
                                </p:column>
                                <p:column>
                                    <p:inputText styleClass="new-fld" id="description"
                                        required="true"
                                        value="#{zipCodeMaintennanceBean.selectedZipCode.description}"
                                        label="Description"></p:inputText>

                                </p:column>
                                <p:column>
                                    <p:message for="description"></p:message>
                                </p:column>
                            </p:row>
                            <f:facet name="footer">
                                <p:row>
                                    <p:column styleClass="text-align:center" colspan="3">
                                        <p:commandButton value="Submit"
                                            update="panel,:#{p:component('display')}"
                                            action="#{zipCodeMaintennanceBean.save}"
                                            oncomplete="if (!args.validationFailed) editDialog.hide()" />
                                        <p:commandButton value="Delete"
                                            update="panel,:#{p:component('display')}"
                                            action="#{zipCodeMaintennanceBean.delete}"
                                            oncomplete="if (!args.validationFailed) editDialog.hide()"
                                            process="@this">
                                        </p:commandButton>
                                        <p:confirmDialog global="true" showEffect="fade"
                                            hideEffect="explode">
                                            <p:commandButton value="Yes" type="button"
                                                styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
                                            <p:commandButton value="No" type="button"
                                                styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
                                        </p:confirmDialog>
                                    </p:column>
                                </p:row>
                                <p:row>
                                    <p:column colspan="3">

                                    </p:column>
                                </p:row>
                            </f:facet>
                        </p:panelGrid>
                    </p:outputPanel>
                </p:dialog>
            </h:form>
        </p:panelGrid>

    </ui:define>
</ui:composition>
</html>

以下是我的Backing Bean:

package com.siteam.web.bean;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.siteam.web.invsys.dao.ZipCodeDao;
import com.siteam.web.invsys.entities.TblZipcodes;

@ManagedBean(name = "zipCodeMaintennanceBean")
@RequestScoped
@Service
public class ZipCodeMaintennanceBean {
    private TblZipcodes selectedZipCode = new TblZipcodes();
    private boolean hasSelectedZipCode = false;
    private List<TblZipcodes> zipCodes = new ArrayList<TblZipcodes>();
    private ZipCodeDao zipCodeDao;
    private boolean hasSearchResult = false;
    private String searchString;
    private boolean addMode;

    public void triggerAdd() {
        init();
        this.addMode = true;
    }

    public String add() {
        try {
            this.selectedZipCode.setDateupdated(Calendar.getInstance()
                    .getTime());

            this.zipCodeDao.add(this.selectedZipCode);

            this.zipCodeDao.refresh(this.selectedZipCode);

            FacesContext context = FacesContext.getCurrentInstance();

            context.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_INFO, "Zip Code Saved. ",
                    "Saved Zip Code"));

            context.getExternalContext().getFlash().setKeepMessages(true);
            init();
            this.addMode = false;

            return "zipmnt";
        } catch (DataIntegrityViolationException ex) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_WARN, "Error:",
                    "ZipCode Already Exists"));
            ex.printStackTrace();
            return "";
        } catch (NullPointerException npe) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Error:", npe.getMessage()));
            npe.printStackTrace();
            return "";
        } catch (Throwable th) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Error:", th.getCause()
                            .getMessage()));
            th.printStackTrace();
            return "";
        }
    }

    public String save() {
        try {

            this.selectedZipCode.setDateupdated(Calendar.getInstance()
                    .getTime());

            this.zipCodeDao.update(this.selectedZipCode);

            this.zipCodeDao.refresh(this.selectedZipCode);

            FacesContext context = FacesContext.getCurrentInstance();

            init();

            // RequestContext.getCurrentInstance().execute("editDialog.hide()");

            context.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_INFO, "Zip Code Saved. ",
                    "Saved Zip Code"));

            context.getExternalContext().getFlash().setKeepMessages(true);
            hasSearchResult = false;
            this.hasSelectedZipCode = false;

            return "zipmnt";
        } catch (DataIntegrityViolationException ex) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_WARN, "Error:",
                    "ZipCode Already Exists"));
            ex.printStackTrace();
            return "";
        } catch (NullPointerException npe) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Error:", npe.getMessage()));
            npe.printStackTrace();
            return "";
        } catch (Throwable th) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Error:", th.getCause()
                            .getMessage()));
            th.printStackTrace();
            return "";
        }
    }

    public String delete() {
        try {
            this.zipCodeDao.delete(this.selectedZipCode);

            FacesContext context = FacesContext.getCurrentInstance();

            init();
            // RequestContext.getCurrentInstance().execute("editDialog.hide()");

            context.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_INFO, "Zip Code Deleted. ",
                    "Deleted Zip Code"));

            context.getExternalContext().getFlash().setKeepMessages(true);
            hasSearchResult = false;
            this.hasSelectedZipCode = false;

            return "zipmnt";
        } catch (DataIntegrityViolationException ex) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_WARN, "Error:",
                    "ZipCode Already Exists"));
            ex.printStackTrace();
            return "";
        } catch (NullPointerException npe) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Error:", npe.getMessage()));
            npe.printStackTrace();
            return "";
        } catch (Throwable th) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Error:", th.getCause()
                            .getMessage()));
            th.printStackTrace();
            return "";
        }
    }

    @PostConstruct
    public void init() {
        this.hasSelectedZipCode = false;
        this.hasSearchResult = false;
        this.addMode = false;
        this.selectedZipCode = new TblZipcodes();
        this.selectedZipCode.setDescription("");
        this.zipCodes.clear();
    }

    public void search() {
        hasSearchResult = false;
        TblZipcodes zipCode = new TblZipcodes();
        zipCode.setZipcode(searchString);

        this.zipCodes = zipCodeDao.findZipCodes(zipCode);

        if (zipCodes.size() > 0) {
            hasSearchResult = true;
        }
    }

    public TblZipcodes getSelectedZipCode() {
        return selectedZipCode;
    }

    public void setSelectedZipCode(TblZipcodes selectedZipCode) {
        this.selectedZipCode = selectedZipCode;
    }

    public boolean isHasSelectedZipCode() {
        return hasSelectedZipCode;
    }

    public void setHasSelectedZipCode(boolean hasSelectedZipCode) {
        this.hasSelectedZipCode = hasSelectedZipCode;
    }

    public List<TblZipcodes> getZipCodes() {
        return zipCodes;
    }

    public void setZipCodes(List<TblZipcodes> zipCodes) {
        this.zipCodes = zipCodes;
    }

    public ZipCodeDao getZipCodeDao() {
        return zipCodeDao;
    }

    @Autowired
    public void setZipCodeDao(ZipCodeDao zipCodeDao) {
        this.zipCodeDao = zipCodeDao;
    }

    public boolean isHasSearchResult() {
        return hasSearchResult;
    }

    public void setHasSearchResult(boolean hasSearchResult) {
        this.hasSearchResult = hasSearchResult;
    }

    public String getSearchString() {
        return searchString;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }

    public void onRowSelect(SelectEvent event) {
        this.selectedZipCode = (TblZipcodes) event.getObject();
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
                "Selected Zip Code", "Zip Code Selected: "
                        + selectedZipCode.getZipcode());
        zipCodeDao.refresh(this.selectedZipCode);

        FacesContext.getCurrentInstance().addMessage(null, msg);

        this.hasSelectedZipCode = true;

    }

    public void onRowUnselect(UnselectEvent event) {
        // FacesMessage msg = new FacesMessage("Car Unselected", ((Car)
        // event.getObject()).getModel());
        this.selectedZipCode = new TblZipcodes();
        this.hasSelectedZipCode = false;
    }

    public boolean isAddMode() {
        return addMode;
    }

    public void setAddMode(boolean addMode) {
        this.addMode = addMode;
    }

}

以下是我的邮政编码实体代码

package com.siteam.web.invsys.entities;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;


@Entity
@Table(name = "TBL_ZIPCODES", schema = "INVSYS")
public class TblZipcodes implements java.io.Serializable {

    private String zipcode;
    private String description = "";
    private Date dateupdated;
    private Set<TblCustomer> tblCustomers = new HashSet<TblCustomer>(0);

    public TblZipcodes() {
    }

    public TblZipcodes(String zipcode) {
        this.zipcode = zipcode;
    }

    public TblZipcodes(String zipcode, String description, Date dateupdated,
            Set tblCustomers) {
        this.zipcode = zipcode;
        this.description = description;
        this.dateupdated = dateupdated;
        this.tblCustomers = tblCustomers;
    }

    @Id
    @Column(name = "ZIPCODE", unique = true, nullable = false, length = 5)
    public String getZipcode() {
        return this.zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

    @Column(name = "DESCRIPTION", length = 50)
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "DATEUPDATED", length = 7)
    public Date getDateupdated() {
        return this.dateupdated;
    }

    public void setDateupdated(Date dateupdated) {
        this.dateupdated = dateupdated;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "tblZipcodes")
    public Set<TblCustomer> getTblCustomers() {
        return this.tblCustomers;
    }

    public void setTblCustomers(Set<TblCustomer> tblCustomers) {
        this.tblCustomers = tblCustomers;
    }   

    @Override
    public boolean equals(Object newObject) {
        return newObject instanceof TblZipcodes;
    }

    // This must return the same hashcode for every Foo object with the same key.
    public int hashCode() {
        return this.getClass().hashCode();
    }

}

提前谢谢。

1 个答案:

答案 0 :(得分:1)

在您的dataTable中,您使用相同的变量进行行选择。

selectionMode="single" selection="#{zipCodeMaintennanceBean.selectedZipCode}"

如果未选择任何行,则selectedZipCode属性将为null,您将继续获得异常。

您可以简单地将另一个TblZipcodes实例用于addDialog。

PS:前段时间有人报告了一个错误,当你在addDialog中给id和widgetV一个名字时,检查这是否是版本3.5中的问题。