Reading the value of g: datePicker in Grails without using the "new object (params)" method

Let's say I have a datePicker called foobar:

<g:datePicker name="foobar" value="${new Date()}" precision="day" />

How did I read the passed value of this date picker?

One way that works, but has some unwanted side effects, is as follows:

def newObject = new SomeClassName(params)
println "value=" + newObject.foobar

This reading method reads all the fields presented in newObject, which I want to avoid. I'm only interested in the value of the foobar field.

I suggested that this is possible:

def newObject = new SomeClassName()
newObject.foobar = params["foobar"]

But Grails does not seem to automatically translate the foobar field into a Date () object.

Updated: More information can be found in Grails JIRA .

+3
4

g: datePicker Grails 1.2 (. ):

 , Date , params (, params.foo )

+2

. , , handleDate. :


def handleDate = { FoobarDateCommand dateCommand ->
    def theNextDay = dateCommand.foobar + 1
}

FoobarDateCommand. , , :


class FoobarDateCommand { 
    Date foobar 
}

Command - , .

+4

When a parameter indicates that it is a "structure", this means that there are several parameters to represent its value. In your case there are:

  • params ["foobar_year" year of storage
  • params ["foobar_month"] month of storage
  • params ["foobar_day"] storage day

Just pull them out and do whatever you want :)

0
source

You still need to know the format, but

def newObject = new SomeClassName()
newObject.foobar = Date.parse('yyyy-MM-dd', params.foobar)
-2
source

Source: https://habr.com/ru/post/1703005/


All Articles