开发者

Using a grails/groovy class by reference

I'm trying to create my own CRUD controller in grails for the stuff that the scaffolding won't do.

Inste开发者_StackOverflow中文版ad of maintaining code for a controller for each domain, I'd like to have one controller that can look after any domain for the generic CRUD calls.. as the only difference is the domain class name.

Using the example of domain class Job & Note

Instead of

Job.get(id)
Job.list()
def instance = new Job(params)

Note.get(id)
Note.list()
def instance = new Job(params)

I was thinking of

def someHandler = Job // configurable

someHandler.get(id)
someHandler.list()
def instance = new someHandler(params)

The first two static methods work fine (get, list) but creating a new instance does not. Any pointers as to how to best do this.

Cheers


Call the default constructor using

def instance = someHandler.newInstance()

and the constructor for params using

def instance = someHandler.newInstance(params)


If you're not happy with the scaffolded controllers/views that Grails provides by default, and want to change them in a similar fashion for all domain classes, a better approach might be to simply edit the templates that are used to generate these controllers/views.

You can do this by running the script grails install-templates. This will create a number of files in the src/templates/scaffolding directory, each of which defines the template used to generate a scaffolded artifact.

Change these templates to create the controllers/views that you want. If you've already run grails generate-all for any domain classes, you'll need to run it again for those classes to update the existing scaffolding.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜