在Groovy / Grails中使模型名称可读 [英] Making model name human readable in Groovy/Grails

查看:151
本文介绍了在Groovy / Grails中使模型名称可读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Groovy / Grails中创建一个可读的域名类名?例如,我可能有一个名为LibraryBook的类,在应用程序视图中,它将显示为Create LibraryBook。是否有可能让Grails将其显示为Create Library Book?

解决方案

如果您查看 grails-app / views / libraryBook / create.gsp 文件,您将看到如下所示的内容:

 < g:set var =entityNamevalue =$ {message(code:'libraryBook.label',default:'LibraryBook')}/> 

这表明您可以设置 libraryBook.label message属性来覆盖 LibraryBook 的默认名称。这个属性应该在 grails-app / i18n / message.properties 文件中设置。此的文档可以在这里找到



作为一个有趣的旁白(而不是推荐的最佳实践),您可以更改默认的grails脚手架模板。首先,您需要安装以下模板:

  grails install-templates 

然后,您可以编辑文件 src / templates / scaffolding / create.gsp (和 list.gsp 等)并更改下列行:

 < g: set var =entityNamevalue =\ $ {message(code:'$ {domainClass.propertyName} .label',default:'$ {className}')}/> 

 < g:set var =entityNamevalue =\ $ {message(code:'$ {domainClass.propertyName} .label',default:'$ {className.replaceAll(/ \B [ AZ] /){$ it}}')}/> 

正如您所看到的,这段代码:

  className.replaceAll(/ \ B [AZ] /){$ it} 

取出CamelCase类名,并用空格字符后的字母替换所有大写字母(除第一个外)。

然后当你调用 generate-views generate-all 时,新创建的gsp将具有这个默认名称,其中包含空格

Is there a way to make a domain class name human readable in Groovy/Grails? For example, I might have a class called LibraryBook and in the application views, it will be shown as "Create LibraryBook". Is it possible to make Grails show this as "Create Library Book"?

解决方案

If you look at the top of your grails-app/views/libraryBook/create.gsp file, you will see something like:

    <g:set var="entityName" value="${message(code: 'libraryBook.label', default: 'LibraryBook')}" />

This shows you can set a libraryBook.label message property to override the default name of LibraryBook. This property should be set in the grails-app/i18n/message.properties file. The documentation for this can be found here.

As an interesting aside (and not the recommended best practice), you can alter the default grails scaffolding templates. First, you need to install the templates with:

grails install-templates

Then, you can edit the file src/templates/scaffolding/create.gsp (and list.gsp, etc) and change the line:

<g:set var="entityName" value="\${message(code: '${domainClass.propertyName}.label', default: '${className}')}" />

to

<g:set var="entityName" value="\${message(code: '${domainClass.propertyName}.label', default: '${className.replaceAll(/\B[A-Z]/){ " $it" }}')}" />

As you can see, this code:

className.replaceAll(/\B[A-Z]/){ " $it" }

Takes the CamelCase classname, and replaces all capital letters (apart from the first one) with the letter following a space character.

Then when you call generate-views or generate-all, the newly created gsp will have this default name with spaces in it

这篇关于在Groovy / Grails中使模型名称可读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆