2014-02-24 40 views
0

我有一個簡單的問題,但我沒有找到它的解決方案。 我有一個簡單的p:selectCheckboxMenu,我想使用selectedDates後點擊按鈕。 「Invaild值」:SelectManyCheckbox選擇轉換日期

f:convertDateTime

<h:form id="mainform"> 
    <p:panelGrid columns="2"> 
     <p:selectCheckboxMenu label="Date" value="#{myBean.selectedDates}"> 
       <f:selectItems value="#{myBean.dates}" var="date" itemValue="#{date}" itemLabel="#{myBean.convertDate(date)}"/> 
       <f:convertDateTime type="date" pattern="dd-MM-yyyy"/> 
      </p:selectCheckboxMenu> 
      <p:commandButton value="Test" actionListener="#{myBean.printDates}"/> 
    </p:panelGrid> 

但比我得到一個錯誤 - 消息試過。

比我嘗試了轉換:

@FacesConverter("myDateConverter") 
public class MyDateConverter extends DateTimeConverter{ 

public MyDateConverter(){ 
    setPattern("MM/dd/yyyy"); 
}} 

<p:selectCheckboxMenu label="Date" value="#{myBean.selectedDates}" converter="myDateConverter"> 

但同樣的錯誤消息。當我使用沒有轉換器我得到「字符串」 - 我的日期列表中的值,因爲類型擦除。

問:我如何得到所選日期爲日期?

這裏是我的bean的完整性:

@ManagedBean(name = "myBean") 
@ViewScoped 
public class MyBean implements Serializable { 

private List<Date> dates; 

private List<Date> selectedDates; 

private SimpleDateFormat dateFormat; 

@PostConstruct 
public void init() { 
    System.out.println("POST CONSTRUCT!"); 
    dateFormat = new SimpleDateFormat("yyyy.MM.dd"); 
    dates = new ArrayList<Date>(); 
    dates.add(new Date()); 
} 

/** 
* 
*/ 
public void printDates(){ 
    for(Date d : selectedDates){ 
     System.out.println(d); 
    } 
} 

/** 
* 
* @param date 
* @return 
*/ 
public String convertDate(Date date){ 
    return dateFormat.format(date); 
} 
+0

嘗試從'f:selectItems'中刪除'itemValue'和'itemLabel'。 – Anas

+0

我有同樣的效果 – pL4Gu33

回答

1

轉換器是問題的根源,因爲它消除了時間,然後將其轉換回日期時使用的默認時間。

可以使用

< F:convertDateTime圖案= 「YYYY-MM-DD HH:MM:SS.SSS Z」/>

或嘗試用< F:datetimeconverter>

+0

謝謝:) ...我也在早上發現了一個鏈接。現在它工作:) http://stackoverflow.com/questions/14046309/jsf-2-fselectitems-with-a-date-keyed-map – pL4Gu33