绑定日期指挥Grails的对象 [英] bind date to command object in Grails

查看:190
本文介绍了绑定日期指挥Grails的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提交日期(作为字符串)。我想这映射到一个命令对象。我环顾四周相当了一下,发现有关如何在命令对象中做映射到一个真正的日期没有很大的资源。

如果我是这样做在控制器​​中我可以做到以下几点,然而这并没有让我轻松地映射到我的命令对象。

  DEF结束日期= params.date('结束日期','MM / DD / YYYY)

对于我的命令对象,我已经能够得到最接近的是覆盖Date对象的getter和setter。都需要被覆盖或其他未使用的设定器。这是我第一次尝试(设置字符串到日期,但得到的日期)。因此,这不使用设定器:

  @ grails.validation.Validateable
类TaskCreateCommand {    日期的startDate
    公共无效setStartDate(字符串dateStr){
        this.start = Date.parse('MM / DD / YYYY',dateStr)
    }}

这不会给任何运行时的问题,但也没用,因为我不能拉出来的实际日期的对象。

  @ grails.validation.Validateable
类TaskCreateCommand {    日期的startDate
    公共无效setStartDate(字符串dateStr){
        this.start = Date.parse('MM / DD / YYYY',dateStr)
    }    公共字符串getStartDate(){
        返回start.toString()
    }
}


解决方案

合作顺便说一句,我今天在看同样的问题,随后的从@Don这个答案。我能够正确地绑定一个日期到命令对象。

  @Validateable
类BookCommand {
    字符串名称
    日期pubdate的
    整页
}//控制器
高清指数(BookCommand CMD){
        的println cmd.name
        的println cmd.pages
        的println cmd.pubDate        渲染完成
    }// SRC /常规
类CustomDateEditorRegistrar实现PropertyEditorRegistrar的{
    公共无效registerCustomEditors(PropertyEditorRegistry注册表){
        日期格式字符串='YYYY / MM / DD'
        registry.registerCustomEditor(日期,新的和CustomDateEditor(新的SimpleDateFormat(日期格式),真))
    }
}// URL
?HTTP://本地主机:8080 / poc_commandObject /书籍名称=测试与放大器;页= 10安培; pubdate的2012年= / 11/11//调用println
测试
10
太阳11月11日00:00:00美国东部时间2012

I have a date (as a string) being submitted. I'd like to map this to a command object. I have looked around quite a bit and found no great resource on how to do this mapping within a command object to a real date.

If I were to do this in the controller itself I could just do the following, however this doesn't allow me to easily map into my command object.

def endDate = params.date('endDate', 'MM/dd/yyyy')

For my command object, the closest I've been able to get is to override the getter and setter for the date object. Both need to be overridden or else the setter is not used. This is what I first tried (set the String to Date, but get the Date). So this doesn't use the setter:

@grails.validation.Validateable
class TaskCreateCommand {

    Date startDate


    public void setStartDate(String dateStr){
        this.start = Date.parse('MM/dd/yyyy', dateStr)
    }

}

This doesn't give any runtime problems, but is useless because I can't pull out the actual Date object.

@grails.validation.Validateable
class TaskCreateCommand {

    Date startDate


    public void setStartDate(String dateStr){
        this.start = Date.parse('MM/dd/yyyy', dateStr)
    }

    public String getStartDate(){
        return start.toString()
    }
}

解决方案

Co-incidentally I was looking at the same problem today, and followed this answer from @Don. I was able to bind a date properly to the command object.

@Validateable
class BookCommand {
    String name
    Date pubDate
    Integer pages
}

//Controller
def index(BookCommand cmd) {
        println cmd.name
        println cmd.pages
        println cmd.pubDate

        render "Done"
    }

//src/groovy
class CustomDateEditorRegistrar implements PropertyEditorRegistrar {
    public void registerCustomEditors(PropertyEditorRegistry registry) {
        String dateFormat = 'yyyy/MM/dd'
        registry.registerCustomEditor(Date, new CustomDateEditor(new SimpleDateFormat(dateFormat), true))
    }
}

//URL
http://localhost:8080/poc_commandObject/book?name=Test&pages=10&pubDate=2012/11/11

//Println
Test
10
Sun Nov 11 00:00:00 EST 2012

这篇关于绑定日期指挥Grails的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆