2014-02-24 44 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