【问题标题】:Convert Date on SelectManyCheckbox Selection在 SelectManyCheckbox 选择上转换日期
【发布时间】:2014-03-26 16:40:45
【问题描述】:

我有一个简单的问题,但我没有找到解决方案。 我有一个简单的p:selectCheckboxMenu,我想在单击按钮后使用 selectedDates。

我试过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);
}

【问题讨论】:

  • 尝试从f:selectItems 中删除itemValue 和itemLabel。
  • 我也有同样的效果

标签: jsf date primefaces converter


【解决方案1】:

转换器是问题的根源,因为它会删除时间,然后在转换回日期时使用默认时间。

你可以使用

或尝试使用

【讨论】:

猜你喜欢
  • 2018-11-23
  • 1970-01-01
  • 2014-08-13
  • 1970-01-01
  • 1970-01-01
  • 2019-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多