将DateRangeSlider中的日期值传递给Grails中的Controller

时间:2014-07-16 07:19:00

标签: jquery jquery-ui grails jquery-plugins params

我在我的GSP中使用jQDateRangeSlider,我有一个名为' mv'的下拉列表。太。我试图将在datelider上选择的日期值传递给我的Controller。这就是我在GSP中的代码的样子:

<g:select from="['AFFILIATES', 'CSE','DISPLAYADS','EMAIL','MOBILEWEB','OTHERS','ORGANIC','SEO', 'SEM']" name="mv" id = "mv" 
           onchange="${remoteFunction(
           controller:'Pgtyp', 
           action:'ajaxGetMv', 
           params:'\'mv=\' + escape(this.value)+\'&date_hour=\'+ z',
           //params:'\'mv=\'+this.value',
           onSuccess: 'printpgtyp(data)')}"
></g:select> 

</form>
<script>
dataFile = "http://localhost:8080/marchmock2/Pgtyp/datejson";

        d3.json(dataFile, function(error,data) {
        if (data) 
                dataset = data;
                var min = dataset[0].a;
                var min2 = new Date(min);
                var max = dataset[dataset.length - 1].b;
                var max2 = new Date(max);
                function addZero(val) {
            if (val < 10) {
                return "0" + val;
            }   
            return val;
            }
                var s = $j("#slider").dateRangeSlider({

            bounds: {
                "min": min2,
                "max": max2
            },
            range: {
                min: {
                    hours: 1
                }
            },
            formatter:function(val){
                var m = moment(val);
                return m.format("DD/MM/YYYY HH:00:00 ");
            },
             defaultValues:{
                min: min2,
                max: max2
            }
        });
                x = (s.dateRangeSlider("values").min);
        x.setMinutes(0);
        x.setSeconds(0);
                z = x.getFullYear()+'-'+(x.getMonth()+1)+ '-'+x.getDate()+' '+x.getHours()+':'+'00'+':'+'00' ;
                var y = (s.dateRangeSlider("values").max);
        y.setMinutes(0);
        y.setSeconds(0);
         $j('#slider').on("userValuesChanged",function (e, data) {
                        var x = data.values.min;
                        x.setMinutes(0);
                        x.setSeconds(0);
                       z = x.getFullYear()+'-'+(x.getMonth()+1)+ '-'+x.getDate()+' '+x.getHours()+':'+'00'+':'+'00' ;
                        var last = data.values.max;
                        last.setMinutes(0);
                        last.setSeconds(0);
                        console.log(last)
             })
        });

这就是我的控制器的样子:

class PgtypController {



    def ajaxGetMv = {
        def pgtyp = Pgtyp.executeQuery("select p.date_hour,p.visits, p.mv, p.browser,p.pagetype,p.platform,p.device,p.time_period from Pgtyp p where p.mv = ? and p.date_hour= ? order by col_0_0_ asc ",[params.mv, params.date_hour])

        render pgtyp as JSON
    }

     def dataSource


        def datejson = {
        def sql = new Sql(dataSource)
        def rows = sql.rows("select min(date_hour) as a , max(date_hour) as b from pgtyp")
        sql.close()
        render rows as JSON
    }

我收到以下错误:

2014-07-16 12:07:29,538 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver  - ClassCastException occurred when processing request: [POST] /marchmock2/pgtyp/ajaxGetMv - parameters:
date_hour: 2014-6-18 10:00:00
mv: OTHERS
java.lang.String cannot be cast to java.util.Date. Stacktrace follows:
Message: java.lang.String cannot be cast to java.util.Date
    Line | Method
->>   11 | doCall    in marchmock2.PgtypController$_closure1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    200 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run       in java.lang.Thread

我哪里错了?

1 个答案:

答案 0 :(得分:1)

根据例外情况,您必须将date_hour字符串转换为Date

DateFormat df = new SimpleDateFormat( 'yyyy-M-dd HH:mm:ss' )
Date d = df.parse params.date_hour