p:日历转换为Object

时间:2014-08-12 19:37:52

标签: oracle date jsf primefaces mybatis

Hei那里,我正在研究一个primefaces应用程序,在我的一个复合组件中,我试图过滤一个表。要实现这一目标,我要使用' Clause'具有列名,运算符和值的对象,它将存储在映射中并发送到mybatis并限制某些选择的输出。

我打印了我的结果,我在p:calendar中使用的转换器输出:

Ok FROM getAsString:     12/08/2014 00:00

但是在我的条款列表中,我得到了这个:

Clause [column=insDate, operator==, value=Tue Aug 12 00:00:00 EEST 2014, columnTitle=Creat la, type=2]

所以当我尝试在持久层中执行它时,我得到了这个:

SQL:SELECT * FROM SSLS_GUI.VW_ITEM WHERE(insDate = TO_DATE(?,' DD / MM / YYYY HH24:MI'))

原因:java.sql.SQLDataException:ORA-01858:找到了数字所在的非数字字符

这是我正在使用的转换器,我决定不再使用Date,而是返回格式化的字符串。但是我不喜欢这些限制(可能在将来我想要改变它并解析没有HH:MI的日期)所以此时我使用了返回formatter.format(date);在getAsObject函数中。

@ManagedBean
@FacesConverter(forClass = Date.class)
public class CalendarConverter implements Converter {

@Override
public Object getAsObject(FacesContext context, UIComponent component,
        String value) {
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    try {
        Date date = (Date) formatter.parse(value);
        System.out.println("OK FROM getAsObj:\t" + date.toString());
        return date;
    } catch (Exception e) {
        System.out.println("EXCEPTION from getAsObj:\t");
    }
    return formatter;
}

@Override
public String getAsString(FacesContext context, UIComponent component,
        Object object) {
    try {
        if (object.toString().equals(" ") || object == null) {
            System.out.println("IS NULL");
            return null;
        } else {
            String date = new SimpleDateFormat("dd/MM/yyyy HH:mm")
                    .format(object);
            System.out.println("Ok FROM getAsString:\t " + date);
            return date;
        }
    } catch (Exception e) {
        System.out.println("EXCEPTION From getAsString\t"
                + object.toString());
        return null;
    }
}

这是p:dataTable用法:

                        <p:calendar
                        rendered="#{cc.attributes.controller.lazyModel.advFilters.dateSelect == true}"
                        value="#{cc.attributes.controller.lazyModel.advFilters.clause.value}"
                        pattern="dd/MM/yyyy HH:mm" required="true"
                        converter="#{calendarConverter}" />

这是查询的一部分:

<foreach item="clause" collection="params1" separator=" AND "
open="(" close=")">                 
    ${clause.column} ${clause.operator}
    TO_DATE(#{clause.value},'DD/MM/YYYY HH24:MI')

0 个答案:

没有答案