2012-06-29 37 views
1

工作:這是我工作的rich:listShuttle,當sourceValue和目標值是字符串列表時。rich:listShuttle使用複雜對象時出錯

1. JSF頁

<rich:listShuttle id="companyJurisdictionShutle" 
      sourceValue="#{companyAdminAction.statesList}" 
      targetValue="#{companyAdminAction.selectedStates}" 
      var="item" orderControlsVisible="false" fastOrderControlsVisible="false" 
      sourceCaptionLabel="Available" 
      targetCaptionLabel="Selected" styleClass="lishShuttle"> 
       <rich:column> 
        #{item} 
       </rich:column> 
</rich:listShuttle> 

2.輔助Bean

//sourceValue      
public List<String> getStatesList() { 

    for (DMPJurisdiction dmpJurisdiction: jurisdictionList) { 
     if(!statesList.contains(dmpJurisdiction.getJurisName())) { 
      statesList.add(dmpJurisdiction.getJurisName()); 
     } 
    } 
    return statesList; 
} 

//targetValue 
public List<String> getSelectedStates() { 
    return selectedStates; 
} 

3.值對象

public class DMPJurisdiction implements Serializable { 

    /** serial version UID **/ 
    private final static Long serialVersionUID = 109892748283726L; 

    /** jurisdiction id **/ 
    private Long jurisId; 

    /** name **/ 
    private String jurisName; 

    /** description **/ 
    private String jurisDescription; 

    /** code **/ 
    private String jurisCode; 

    //Getters and Setters 

} 

NOT-WORKING:我更改了列表梭,以便sourceValue和targetValue是複雜對象(DMP管轄區)的列表,而不像以前那樣是Strings列表。爲此我寫了一個轉換器。

1. JSF頁面

<rich:listShuttle id="companyJurisdictionShutle" 
     sourceValue="#{companyAdminAction.jurisdictionList}" 
     targetValue="#{companyAdminAction.targetJurisdictionList}" 
     converter="#{dmpJurisdictionConverter}" 
     var="item" orderControlsVisible="false" fastOrderControlsVisible="false" 
     sourceCaptionLabel="Available" 
     targetCaptionLabel="Selected" styleClass="lishShuttle"> 
      <rich:column> 
       #{item.jurisName} 
      </rich:column> 
</rich:listShuttle> 

2.輔助Bean:現在回到上面列出的複雜對象DMPJurisdiction的名單。

//sourceValue 
public List<DMPJurisdiction> getJurisdictionList() { 
    return jurisdictionList; 
} 

//targetValue 
    public List<DMPJurisdiction> getTargetJurisdictionList() { 
    return targetJurisdictionList; 
} 

3.轉換

public class DmpJurisdictionConverter implements javax.faces.convert.Converter { 

     public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) { 
      List<DMPJurisdiction> dmpJurisdictionList = Cache.loadAllDmpJurisdictions(); 
      for (DMPJurisdiction dmpJurisdiction : dmpJurisdictionList) { 
       if (dmpJurisdiction.getJurisName().equals(s)) { 
        return dmpJurisdiction; 
       } 
      } 
      return null; 
     } 

     public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) { 
      List<DMPJurisdiction> dmpJurisdictionList = Cache.loadAllDmpJurisdictions(); 
      for (DMPJurisdiction dmpJurisdiction : dmpJurisdictionList) { 
       if (((DMPJurisdiction) o).getJurisName().equals(dmpJurisdiction.getJurisName())) { 
        return dmpJurisdiction.getJurisName(); 
       } 
      } 
      return null; 
     } 

} 

4.錯誤:sourceId=accountWorkcaseOpenTabForm:addDmpCompanySubview:companyJurisdictionShutle[severity=(ERROR 2), summary=(javax.el.PropertyNotFoundException: /html/workcase/type/dmp/admin/AddCompany.xhtml @55,98 sourceValue="#{companyAdminAction.jurisdictionList}": Property 'jurisdictionList' not writable on type java.util.List), detail=(javax.el.PropertyNotFoundException: /html/workcase/type/dmp/admin/AddCompany.xhtml @55,98 sourceValue="#{companyAdminAction.jurisdictionList}": Property 'jurisdictionList' not writable on type java.util.List)] ||||

注:只是一個方面說明,我使用的是相同的dmpJurisdictionConverter成功地爲selectOneMenu用於如圖所示在不同的下面不相關的 JSF頁面。

<h:selectOneMenu value="#{companyAdminAction.dmpJurisdiction}" converter="#{dmpJurisdictionConverter}"> 
        <s:selectItems var="item" value="#{companyAdminAction.jurisdictionList}" label="#{item.jurisName}" 
            hideNoSelectionLabel="true" noSelectionLabel="-- Select Jurisdiction --"/> 
        <a4j:support event="onchange" action="#{companyAdminAction.loadCompanyList()}" 
          reRender="dmpCompanies"/> 
      </h:selectOneMenu> 

回答

0

我被加載sourceValue =「 # {companyAdminAction.statesList}「使用其獲取器getStatesList。它應該單獨加載,因爲在頁面渲染時,會調用getter和setter多次。我必須將stateList加載到另一個在bean初始化期間調用的私有方法。

1

可以使用接縫標籤「< 」S:convertEntity />

它使自動轉換,但複雜的對象必須是一個實體