问题
I'm a newbie on grails. I am now currently working on my scaffolding templates especially on my controllers. I wanted a customized controller everytime I generate it so I used "install-templates". I always create a Command object on controllers, is it possible to include the fields from my domain class to my command object in my generated controller? I know I have to do it in controller templates but i dont know how to code it or if it is even possible. So everytime I use generate-controllers, the fields on the Command Object is already set.
${className}Command implements java.io.Serializable{
constraints = {}
}
and for example my Domain class looks like this:
class Person{
String name
int age
double height
}
I want it to automatically generate the fields of my domain class in the Command object on my generated controller through editing the controller template. is it possible? thanks for sharing your knowledge.
回答1:
edit.gsp
, show.gsp
and list.gsp
templates all have logic for creating fields basing on domain class, you can see there how it's done.
Basically, when you include groovy code in your template, you can access domain class by using domainClass
variable, and then you can print properties declarations by iterating over array returned by getProperties(), like this:
<%
domainClass.properties.each {
println " ${it.type} ${it.name}"
}
%>
回答2:
Interesting question - why do you think you need the command object? This way, you are violating the dry principle.
As you are just starting with grails, you should just try to use grails as it is inteded to be used and not try to enhance it.
As soon as you've created your first fully featured grails project, you'll see the beauty of the grails design - without the need of implicit command objects :-) - or chose another framework
来源:https://stackoverflow.com/questions/9868845/grails-templates-scaffolding-controller