We're migrating from Grails 2.x to 3.x. I can observe some different behaviour when using the forward function:
class FooController {
def index() {
forward controller: 'foo', action : 'bar', params: params
}
def bar() {
render(
view: "/foo/foo"
)
}
}
When calling http://localhost:8080/foo?test=1 and halting in the bar() method I can see that params looks like that:
params = {GrailsParameterMap#11597} size = 4
0 = {LinkedHashMap$Entry#11607} "test" ->
key = "test"
value = {String[2]#11612}
0 = "1"
1 = "1"
1 = {LinkedHashMap$Entry#11608} "controller" -> "foo"
2 = {LinkedHashMap$Entry#11609} "format" -> "null"
3 = {LinkedHashMap$Entry#11610} "action" -> "bar"
As you can see the value of test is saved twice as a String[]. This behaviour is different than it used to be in Grails 2.5.6.
Is there any way to set a flag to the Grails forward function in order to not get the params passed to the redirect controller?
I think you don't need to add the param. forward automatically forwards your parameters. It's optional. If you add it, it will duplicate the values. Try with only:
forward controller: 'foo', action : 'bar'
Related
I've been trying to take advantage of operator overloading in Groovy by defining a custom putAt method in my POGO like this:
class Book {
Map additionalInfo = [:]
def putAt(key, value) {
additionalInfo[key] = value
}
}
So that I can do something like, book['notes'] = 'I like this one.' (let's say this makes sense). However, I've been getting:
groovy.lang.MissingPropertyException: No such property: notes for class: Book
at BookSpec.Set property using putAt(BookSpec.groovy:40)
My class is part of a Grails application so I'm not sure if Grails has something to do with the problem. Can anyone enlighten me on this?
The signature should be
def putAt(String key, value)
Instead of doing putAt and then override the operator, there is an easy/better way by adding the propertyMissing methods.
Here is the link for more explanation.
class Foo {
def storage = [:]
def propertyMissing(String name, value) { storage[name] = value }
def propertyMissing(String name) { storage[name] }
}
def f = new Foo()
f.foo = "bar"
assertEquals "bar", f.foo
Assume I have the following Foo object:
Foo foo = new Foo(foo: "foo", bar: "bar", baz: "baz")
I know how to validation specific constraints:
foo.validate(["foo", "bar"]) // validates the "foo" property and the "bar" property, but not the "baz" property
I also know how to forego validation all together:
foo.save(validate: false)
But I don't know how to tell Grails to validate all constraints except those in a list. I could make a method that does what I want, but I wanted to make sure there wasn't a Groovy way to do it first.
Update
Here is how I will do this if there isn't a "groovier" way.
// This method exists in my Util.groovy class in my src/groovy folder
static def validateWithBlacklistAndSave(def obj, def blacklist = null) {
def propertiesToValidate = obj.domainClass.constraints.keySet().collectMany{ !blacklist?.contains(it)? [it] : [] }
if(obj.validate(propertiesToValidate)) {
obj.save(flush: true, validate: false)
}
obj
}
Considering the below Foo domain class
class Foo {
String foo
String bar
String baz
static constraints = {
foo size: 4..7
bar size: 4..7
baz size: 4..7
}
}
Validation for baz can be excluded as follows:
Foo foo = new Foo(foo: "fool", bar: "bars", baz: "baz")
//Gather all fields
def allFields = foo.class.declaredFields
.collectMany{!it.synthetic ? [it.name] : []}
//Gather excluded fields
def excludedFields = ['baz'] //Add other fields if necessary
//All but excluded fields
def allButExcluded = allFields - excludedFields
assert foo.validate(allButExcluded)
assert foo.save(validate: false) //without validate: false, validation kicks in
assert !foo.errors.allErrors
There is no direct way to send a list of excluded fields for validation.
You can define customized constraints maps, which you can then effectively filter from either supporting command classes or Config.groovy via exclude parameter.
Is their a dynamic namedquery on grails? Im not sure if its the right term but, I mean a namedquery that can be true to all.
Something like:
namedQueries = {
dynamicQuery{ term, name, value ->
term(name, value)
}
}
Then it can be called maybe like but not exactly:
def testClass = TestClass.dynamicQuery('eq', 'lastname', 'Bill').list()
and so you call it too like:
def testClass = TestClass.dynamicQuery('gt', 'id', 12).list()
This one might not work but is their something similar in grails?
UPDATE
The idea is that so I can chained it as many as I want like:
def testClass = TestClass.dynamicQuery('gt', 'id', 12).dynamicQuery('eq', 'stat', 11).list()
This is so that I dont have to create many namedqueries. I was hoping I can create one and use it multiple times.
Grails' createCriteria method generates Grails HibernateCriteriaBuilder instance, within which you can call invokeMethod method to dynamically create query criteria, which usually is defined by the standard DSL.
Here is a example in some controller:
private String dynamicCriteriaTest(String term, name, value) {
def c = TestClass.createCriteria()
def param = []
param << name
param << value
def result = c.list{
c.invokeMethod(term, param as Object[])
}
return result.toString()
}
def test() {
render dynamicCriteriaTest('eq','lastname','Bill')
}
That will get something you want.
update
If you want to call this method multiple times, pass the criteria parameters in an a List then execute the query:
private List dynamicCriteriaTest(List param) {
def c = TestClass.createCriteria()
def paramList = param.collate(3) //split the parameters into groups
def result = c.list{
paramList.each { paramInstance ->
def command = paramInstance[0]
paramInstance.remove(0)
c.invokeMethod(command, paramInstance as Object[])
}
}
return result
}
def test() {
ArrayList param = new ArrayList()
//the 1st criteria
param << 'gt'
param << 'id'
param << (long)12 //you have to check the Grails [HibernateCriteriaBuilder] API to make sure the parameter passed to `invokeMethod` is in the right type (e.g. **long** in this case)
//the 2nd one
param << 'eq'
param << 'stat'
param << (long)11
//even more
param << 'like'
param << 'description'
param << 'some text%'
render dynamicCriteriaTest(param)
}
In Grails you have NamedQueries and also Where Queries. The example you give can possibly be implemented by using a namedqueries and placing this in a abstract domain class. Your domain classes should extend this abstract domain.
I have a controller like this :
def unCompletedTasks() {
def user = User.get(springSecurityService.principal.id)
def choice = params.managersProject
params.max = Math.min(params.max ? params.int('max') : 10,100)
def search = Tasks.createCriteria().list(max: params.max as Integer, offset: params.offset as Integer, order: params.order as String, sort : params.sort) {
and {
project {
like('name',"${choice}")
}
eq('completed',false)
lt('endDate',new Date().clearTime())
}
}
[tasksInstanceList : search, tasksInstanceTotal: search.getTotalCount() ]
}
I want to test this. I wrote a test specification in Spock like this:
def 'user should be displayed unCompletedTasks' () {
setup: "set the required objects"
def tasksController = new TasksController()
tasksController.springSecurityService = [principal: [id:tasksInstance.id]]
tasksController.params.managersProject = "testing"
//other codings goes here
when:
def model = tasksController.unCompletedTasks()
then:
model.tasksInstanceTotal == 1
where:
//required fields
}
When I run, I get a error like this :
user should be displayed unCompletedTasks(mnm.schedule.TasksSpec)
| org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'null' with class 'org.codehaus.groovy.runtime.NullObject' to class 'java.lang.Integer'
at mnm.schedule.TasksController.unCompletedTasks(TasksController.groovy:39)
at mnm.schedule.TasksSpec.user should be displayed unCompletedTasks(TasksSpec.groovy:59)
I don't know where I went wrong.
Thanks in advance.
Sounds like the same problem as discussed in Problems casting a null object with Spock. The solution is to upgrade to Grails 2.0.2.
My Grails code has a search function that redirects to another controller action after performing a findAllBy query:
def results = Foo.findAllByBar(baz)
redirect(action: "result", params: [results: results])
findAllByBar returns an ArrayList with models, as expected, but after the redirect the receiving action gets a String array. Worse, when there is only one result it doesn't even get an array, it just gets a String.
Given that I have to iterate through the results in the receiving view, doing it on a string will meticulously print every letter individually. We can all agree that that's probably not the ideal behaviour.
A redirect results in a new GET request with the parameters in the querystring, e.g. /controller/result?foo=bar&baz=123 - you can't put objects there since it's just a string.
You could put the ids of the objects in the params and load them in the result action:
def action1 = {
def results = Foo.findAllByBar(baz)
redirect(action: "result", params: [resultIds: results.id.join(',')])
}
def result = {
def resultIds = params.resultIds.split(',')*.toLong()
def results = Foo.getAll(resultIds)
}
or put them in Flash scope:
def action1 = {
flash.results = Foo.findAllByBar(baz)
redirect(action: "result")
}
def result = {
def results = flash.results
}
It sounds like you want to use the chain method instead of the redirect method. Chain lets you pass a model as a parameter similar to render.
An example would be:
chain(action:'result',model:[results:results])
Heres a link for further information:
http://www.grails.org/doc/latest/ref/Controllers/chain.html