修改脚手架模块生成工件

In one of the sessions in SpringOne2GX, there was a session on Uber-Scaffolding by Jean Barmarsh. The session was quite incredible and opened up a world of possibilities. We all know that the scaffolded code generated by grails is modifiable if we install the base templates. This is done simply by saying:

1 grails install-templates

The above command will create the following directory structure in the application’s src folder


As we can see, there are 3 main directories here :

  1. Artifacts: This directory contains all the base files for creating various artifacts. For example, if you say “grails create-domain-class packageName.className”, then the file “src/artifacts/DomainClass.groovy” is taken as the template for creating this file. Same goes for other artifacts like filters, taglibs, controllers, etc.
  2. Scaffolding: This directory contains the template files that are used for generating scaffolded code(controllers and views).
  3. War : This just contains the web.xml file. If you need to add some configuration block(e.g. session-timeout, etc)

Now there are a lot of things that we may need to modify in the grails templates, the pagination size being one. So, the first change that I did was made the “max” parameter in controller be Config driven simply by replacing :

1 params.max = Math.min(params.max ? params.int('max') : 10100)

by

1 int pageSize = grailsApplication.config.grails.scaffolds.pageSize ?: 10
2 params.max = Math.min(params.max ? params.int('max') : pageSize, 100)

This was the simplest thing that was done.

In order to use the changed scaffolding, there are two ways of doing this:

  1. 1 grails generate-all MyDomainClass
  2. Create an empty controller, and put the following code in it :
    1 def scaffold = MyDomainClass

Note: When playing around with grails templates, be very careful not to format this code using an IDE. The formatting will add unwanted spaces and the grails create-*/generate-* commands will start failing or produce wrong/unusable files.

Will be back with more :)

Regards
~~Himanshu Seth~~

http://www.IntelliGrape.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值