2011-05-03 45 views
2

我想將默認值設置爲h:selectOneMenu。但是,它不起作用。如何將默認值設置爲<h:selectOneMenu>

這是我的代碼

的index.xhtml

<h:body> 
     <h:form id="test"> 
     <h:selectOneMenu value="#{selectMenuBean.selectedItem}" 
         title="select version" 
         onchange="submit()" 
         disabled="false" id="combo"> 

      <f:selectItems value="#{selectMenuBean.selectItems}" /> 
     </h:selectOneMenu> 

     </h:form> 
    </h:body> 

BackingBean

private String selectedItem; 
private List selectItems; 
private int version=3; 

public List getSelectItems() { 

    List<Version> selectedItems = ExportDao.getVersionsList(); 
    System.out.println("List size: "+selectedItems.size()); 
    selectItems = new ArrayList(); 

    for (Version v1 : selectedItems) { 
     String DATE_FORMAT = "yyyy-MM-dd HH:mm"; 
     //Create object of SimpleDateFormat and pass the desired date format. 
     SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); 
     selectItems.add(new SelectItem(v1.getVersion(), "V" + v1.getVersion() + "/" + sdf.format(v1.getDate()))); 
     if(version = v1.getVersion()) // I have to check the version and set the matching version as selected. 
     selectedItem = "V" + v1.getVersion() + "/" + sdf.format(v1.getDate()); 

    } 
    return selectItems; 
} 

回答

3

一些可能的解決方案:

1)設置的類型selectItems改爲SelectItem[],而不改爲List

或2)嘗試設置下面的selectItems的var,itemValue和itemLabel屬性,並將實際的Version對象放入列表中。

或我的最愛,3)製作一個VersionConverter,知道如何將Version對象轉換爲字符串。以下示例說明如果您的Version對象在數據庫中持久存在且具有Id。在構建完成之後,您的selectedItem和List selectItems應該具有typ(和List),而不是String。 JSF將自行處理轉換。

@FacesConverter(forClass=Version.class) 
public class VersionConverter implements Converter{ 

    public VersionConverter() { 
    } 

    @Override 
    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) { 
     if (value == null || value.length() == 0) { 
      return null; 
     } 
     try { 
      // Get an EJB that can fetch the Version from a DB. Alternativly, do whatever you need to get your object from a string. 
      InitialContext ic = new InitialContext(); 

      MyDao myDao = (MyDao)ic.lookup(String.format("java:global/%s/MyBean", (String)ic.lookup("java:module/ModuleName"))); 
      return myDao.findEntity(Version.class, getKey(value)); 
     } catch (NamingException e) { 
      return null; 
     } 
    } 

    Long getKey(String value) { 
     Long key; 
     key = Long.valueOf(value); 
     return key; 
    } 

    String getStringKey(Long value) { 
     StringBuilder sb = new StringBuilder(); 
     sb.append(value); 
     return sb.toString(); 
    } 

    @Override 
    public String getAsString(FacesContext facesContext, UIComponent component, Object object) { 
     if (object == null) { 
      return null; 
     } 
     if (object instanceof Version) { 
      Version e = (Version) object; 
      return getStringKey(e.getId()); 
     } 
     else 
      throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Version.class.getName()); 
    } 
} 
+0

您好弗蘭克先生更換

selectedItem = "V" + v1.getVersion() + "/" + sdf.format(v1.getDate()); 

,謝謝你的回覆。我會嘗試這個解決方案。 – neni 2011-05-04 05:34:09

4

您正在使用項目標籤而不是項目值設置selectedItem

通過

selectedItem = v1.getVersion(); 
+0

嗨BalusC,我已經改變了它,就像你說的。但是,沒有運氣。我正在打印selectedItem。它正在打印所選的一個,但不是設置值。 – neni 2011-05-04 05:31:09