Extract template code from controller
I am literally just getting started with the grail and just creating some forests. There seems to be some pretty generic generic code here that will be repeated in every controller.
- success test get ()
- optimistic lock check
How do you recommend removing this from the controller? Ideally I would like to just do
def personInstance = Person.get(id)
and then a single exception handler does for each controller what is thrown by default in each controller.
def update(Long id, Long version) {
def personInstance = Person.get(id)
if (!personInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'person.label', default: 'Person'), id])
redirect(action: "list")
return
}
if (version != null) {
if (personInstance.version > version) {
personInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[message(code: 'person.label', default: 'Person')] as Object[],
"Another user has updated this Person while you were editing")
render(view: "edit", model: [personInstance: personInstance])
return
}
}
source to share
If you want to change the default scaffolding for a controller, simply issue the "grails install-templates" command. A lot of files will be created in the src / templates folder. And one of them is "src / templates / scaffolding / Controller.groovy"
Then just change the "update" function in the format you want and generate the controller again for your domain classes.
However, you should consider whether you want to use optimistic / pessimistic locking as it makes your application non-transactional in some way.
source to share
Check out the template for simplifying Grails controllers , it offers a Groovy template for this stuff. Let us know if you come up with a good solution, I'm going to do something similar to myself.
source to share