2013-08-06 45 views
0

我遇到了我創建的網站有問題。我是Faces 2.0的新手,只使用JSP,現在轉向Faces 2.0。我有一個頁面添加和刪除一個位置郵政編碼。編輯部分作品,但附加部分,我有一個錯誤:在下面的值PrimeFaces PropertyNotFoundException目標無法訪問,'null'返回null

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> 

下面是我支持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

我得到了這個問題解決,我把對話框「添加」組件到一個新的窗體之外的窗體,與編輯對話框共享,不知道爲什麼它的工作,以及不知道爲什麼它不工作在第一名 – yojstamaria

+0

很高興你解決了這個問題。有沒有理由不能使用@ViewScoped?似乎它在這裏可能更自然。 – Cody

+0

我實際上已經嘗試了RequestScoped,SessionScoped和ViewScoped,但並沒有工作,但我想我會在這一個上使用RequestScoped,我將從我的其他任務開始,因爲我被擱置在這一個上。 – yojstamaria

回答

1

在你的dataTable中,你正在使用同一個變量進行行選擇。

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

如果沒有行選擇selectedZipCode屬性將是無效和你將繼續得到一個異常。

您可以簡單地爲addDialog使用另一個TblZipcodes實例。

PS:前段時間有人在給id和widgetVar使用相同名稱時會報告一個bug,就像你在addDialog中做的一樣,檢查這是否是版本3.5中的問題。

+0

每當addDialog對話框打開時,我實際上都不會使用dataTable,因爲這一行它不會被渲染: rendered =「#{zipCodeMaintennanceBean。hasSearchResult}「 它在addDialog上的inputText組件上,只要我刪除它的值,它就會加載,但我的代碼不會工作 我試圖刪除對話框上的Id並且也不起作用 – yojstamaria

+0

我明白了!您嘗試將bean的範圍更改爲ViewScoped? –

相關問題