0

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

PropertyNotFoundException, Target Unreachable, 'null' returned null.

关于以下值:

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

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

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

我怀疑每当我单击按钮时,都会创建一个新对象,因为我正在使用 @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>

下面是我的支持 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();
    }

}

先感谢您。

4

1 回答 1

1

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

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

如果未选择任何行,则 selectedZipCode 属性将为空,您将继续收到异常。

您可以简单地为 addDialog 使用另一个 TblZipcodes 实例。

PS: 前段时间有人报告给id和widgetVar取同名的时候和addDialog一样的bug,看看是不是3.5版本的问题。

于 2013-08-06T11:56:06.837 回答