简体   繁体   English

将日期绑定到Grails中的命令对象

[英]bind date to command object in Grails

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. 对于我的命令对象,我能够得到的最接近的是覆盖日期对象的getter和setter。 Both need to be overridden or else the setter is not used. 两者都需要被覆盖,否则不使用setter。 This is what I first tried (set the String to Date, but get the Date). 这是我第一次尝试(将String设置为Date,但获取Date)。 So this doesn't use the setter: 所以这不使用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. 这不会给出任何运行时问题,但是没用,因为我无法提取实际的Date对象。

@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()
    }
}

If you're using Grails 2.3 or later, you can add multiple custom date formats for data binding in your Config.groovy 如果您使用的是Grails 2.3或更高版本,则可以在Config.groovy中为数据绑定添加多种自定义日期格式

grails.databinding.dateFormats = ["MM/dd/yyyy"]

Then when Grails attempts to bind the request parameters to Command object properties, your custom format will be tried when parsing the date String in the request parameters eg. 然后,当Grails尝试将请求参数绑定到Command对象属性时,将在解析请求参数中的日期字符串时尝试自定义格式,例如。

class MyController {
    def myAction(MyCommandObject cmd) {
        println cmd.date
        println cmd.date.class
    }
}

class MyCommandObject {
    Date date
}

then when you hit eg. 然后当你击中eg。

http://localhost:8080/myController/myAction?date=12/13/2013

you should see something like -> 你应该看到类似的东西 - >

Fri Dec 13 00:00:00 EST 2013
class java.util.Date

on your console. 在你的控制台上。

Co-incidentally I was looking at the same problem today, and followed this answer from @Don. 共顺便我今天在看同样的问题,其次从@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

Worked for me if using the correct @BindingFormat. 如果使用正确的@BindingFormat,我可以为我工作。 Example: 例:

import grails.databinding.BindingFormat
class DateRangeFilter {
    String fieldName
    @BindingFormat('yyyy-MM-dd')
    Date from
    @BindingFormat('yyyy-MM-dd')
    Date to 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM