rich中的日期项:pickList

时间:2014-10-31 11:15:43

标签: jsf jsf-2 richfaces

我使用RichFaces组件 rich:pickList 来提供可以选择的Date对象列表

当数据得到验证时,JSF页面显示验证错误:“验证错误:值无效”。我知道,Date Object不知何故没有被转换。但我不知道为什么? 首先我尝试设置属性dateConverter:

converter="#{javax.faces.convert.DateTimeConverter}"

这不起作用。之后,我添加了以下声明:

<f:convertDateTime pattern="MM/dd/yyyy" />

也没有解决问题。

顺便问一下:两者有什么区别?

请在下面找到完整的源代码:

XHTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:h="http://java.sun.com/jsf/html" 
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets" 
      xmlns:a4j="http://richfaces.org/a4j"
      xmlns:rich="http://richfaces.org/rich">

    <ui:composition template="/templates/template.xhtml">
        <ui:define name="body">
            <h:form prependId="false">
                <rich:pickList id = "datePL" 
                               value="#{richBean.listOfNewDates}"  
                               sourceCaption="All Dates" 
                               targetCaption="New Dates" 
                               >
                    <f:selectItems value="#{richBean.listOfDates}"
                                   var="_item" itemValue="#{_item}" itemLabel="#{_item}" />
                        <f:convertDateTime pattern="MM/dd/yyyy" />

                </rich:pickList>

                <a4j:commandButton action="#{richBean.printListOfDates()}" value="Print"/>

                <rich:notifyMessages stayTime="2000" nonblocking="true" />

            </h:form>
        </ui:define>
    </ui:composition>
</html>

托管Bean:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.SessionScoped;
import javax.inject.Named;

@Named("richBean")
@SessionScoped
public class RichBean implements Serializable {

    private static final long serialVersionUID = -2403138958014741653L;
    private List<Date> listOfDates;
    private List<Date> listOfNewDates;

    @PostConstruct
    public void init() {
        setListOfDates(new ArrayList<Date>());
        Calendar cal = Calendar.getInstance();
        getListOfDates().add(cal.getTime());
        cal.add(Calendar.DATE, 1);
        getListOfDates().add(cal.getTime());
        cal.add(Calendar.DATE, 2);
        getListOfDates().add(cal.getTime());
    }

    /**
     * @return the listOfDates
     */
    public List<Date> getListOfDates() {
        return listOfDates;
    }

    /**
     * @return the listOfNewDates
     */
    public List<Date> getListOfNewDates() {
        return listOfNewDates;
    }

    /**
     * @param listOfNewDates the listOfNewDates to set
     */
    public void setListOfNewDates(List<Date> listOfNewDates) {
        this.listOfNewDates = listOfNewDates;
    }

    public void printListOfDates() {
        for (Iterator<Date> it = listOfNewDates.iterator(); it.hasNext();) {
            System.out.println("Neues Datum = " + it.toString());
        }
    }

    /**
     * @param listOfDates the listOfDates to set
     */
    public void setListOfDates(List<Date> listOfDates) {
        this.listOfDates = listOfDates;
    }
}

服务器端没有异常。

请问有人帮忙吗?这太好了!提前谢谢!

1 个答案:

答案 0 :(得分:0)

我在另一篇文章中找到了解决方案:JSF 2 - f:selectItems with a Date keyed Map

不知道为什么我事先没有找到这篇文章..