- 浏览: 95455 次
- 性别:
- 来自: ...
最新评论
-
woshidashuaibi:
Grails从零开始开发项目二 -
ncc:
你应该把import也帐号出来复制的代码调不能......照 ...
Groovy新发布的 HTTPBuilder 模块 -
craengjava:
我这里不能显示图片,也没有报什么错误。
给Grails添加验证码插件 -
fenglin_Java:
能问个问题吗?能大概说下你做项目的时候注册是怎么做的吗?验证怎 ...
Grails从零开始开发项目四 -
wenxiang_tune:
谢谢,灰常不错,我将继续关注。。。。
Grails从零开始开发项目四
i18n机制
Grails提供了健全的i18n机制,具体文件看看/grails-app/i18n目录就明白。默认会生成各个语言的properties文件。
Grails的很多地方都与i18n有关。
其中包括,域对象的约束,控制层的返回错误信息,gsp中的字符显示。
对象的约束:
blank约束对应于className.propertyName.blank properties文件中的 key,className和propertyName是你的类名称和属性名称
login(blank:false)
className.propertyName.blank
Controller里你会看到返回的错误信息。
读取properties文件中 default.created.message 这个key信息。
flash.message = "${message(code: 'default.created.message', args: [message(code: 'pricing.label', default: 'Pricing'), pricingInstance.id])}"
在gsp文件中一些字符需要用taglib从properties文件中读取。
<g:message code="default.list.label" args="[entityName]" />
这些工作很细碎,繁杂。还好Grails非常强大的插件机制和脚手架概念。
所以有一款插件:i18n-templates,可以帮你生成这些i18n需要的代码。
在安装i18n-templates插件之前,首先安装Grails的脚手架模板,
grails install-templates
然后安装i18n-templates
grails plugin-install i18n-templates
i18n-templates帮你把Grails的脚手架页面中所有硬code的信息都转换成从i18n中读取的信息,所以你在使用
grails generate-all ,grails generate-controller, grails generate-views 跟脚手架有关的命令时它都会调用i18n-templates脚手架模板文件,生成包括i18n的gsp和controller文件。
同时它提供了一个新命令,帮你吧所有i18n的字段和内容生成好。但你还需要吧这些信息copy到i18n文件中。
grails generate-i18n-messages <domainClass>
grails generate-i18n-messages "*"
这个命令读取你的域对象,生成域所需要的properties信息,前提是你必须把每个字段都放在constraints中,哪怕是空呢。
具体看插件介绍页面。
http://grails.org/plugin/i18n-templates
Grails插件
Grails的一个优点就是社区建设的比较完善,提供了非常完美严谨各种机制,这让很多的人都融入到Grails社区建设中来。其中插件机制是非常重要的一块,在web开发各个领域优秀的开源项目基本都有相应的Grails插件。
下面是我整理了一些不错的插件,并简单介绍一下怎么用这些插件。
blueprint-css
:css插件
file-uploader
: 一个简单的上传插件
i18n-templates
: 前面说过了
jquery
: 基本web开发离不开的js框架,也是最好用的js框架
lookups
:
http://www.grails.org/Lookups+Plugin 处理静态变量的插件。会吧静态数据放在properties 文件中,并管理他们。
例如:性别属性的内容,男女,地区的内容=可能就是几十个省市。用来保存缓存修改这种数据。
每一个只对应Lookup 和LookupValue字段,并分别保存在两个properties 文件中。
修改Config.groovy中的 lookups.cache.size.kb = 32 值,更改lookups缓存的大小
通过这两个地址管理:
http://localhost:8080/Taurus/lookup 添加数组的定义
http://localhost:8080/Taurus/lookupValue 添加数组中的数据
<g:lookupSelect id="status" name="status" realm="order.status" value="${sales.status}"/> <g:lookupValue realm="order.status" value="${sales.status}"/> def lst = Lookup.codeList("order.status") // List of code/value pairs as maps def val = Lookup.valueFor("order.status", 23) // The value for code 23 static constraints = { status(blank:false, validator: {val, obj -> return Lookup.valueFor("order.status", val) != null }) }
message-digest
: 提供一系列编码
加密编码,支持SHA1 and SHA1Hex MD5,使用非常简单就下面四种。
"some string".encodeAsSHA1()
"some string".encodeAsSHA1Hex()
"some string".encodeAsMD5()
"some string".encodeAsMD5Hex()
pdf
: 将普通的页面输出成pdf,如果要求不高这个插件足够
tag插件
:
http://www.grails.org/plugin/taggable
拥有tag的实体需要继承下面接口
import org.grails.taggable.* class Vehicle implements Taggable { }
tag插件提供的一些方法
//添加TAG def v = Vehicle.get(1) v.addTag("red") .addTag("sporty") .addTag("expensive") // Alternatively v.setTags(['red', 'sporty', 'expensive']) // Or v.addTags(['electric', 'hybrid']) ///查询: def v = Vehicle.get(1) println v.tags def vehicles = Vehicle.findAllByTag("sporty") // Also takes params eg [max:5] def count = Vehicle.countByTag("sporty") assert 3 == Vehicle.totalTags assert ['expensive', 'red','sporty'] == Vehicle.allTags //Tag分析: def tags = "red,sporty,expensive" def v = Vehicle.get(1) v.parseTags(tags) assert ['expensive', 'red','sporty'] == v.tags tags = "red/sporty/expensive" v.parseTags(tags, "/") assert ['expensive', 'red','sporty'] == v.tags //域对象的其他方法: getAllTags getTotalTags countByTag { String tag -> findAllByTag { String name-> findAllByTag { String name, Map args-> findAllByTagWithCriteria { String name, Closure crit -> findAllTagsWithCriteria { Map params, Closure criteria ->
在配置文件中配置tag的数据库表名等信息,Config.groovy:
grails.taggable.tag.table="MY_TAGS"
grails.taggable.tagLink.table="MY_TAG_LINKS"
image-tools
:
http://grails.org/plugin/image-tools 超好用的图片处理工具,没啥说的,好,基于JAI。早就在找这东西了~~
安装方法:
grails install-plugin http://www.arquetipos.co.cr/blog/files/grails-image-tools-1.0.4.zip
def imageTool = new ImageTool() imageTool.load(f.getBytes()) //载入二进制图片数据 imageTool.load("/path/to/image.jpg") //从文件载入图片 imageTool.saveOriginal() //保存 imageTool.thumbnail(640) // 缩略图 imageTool.writeResult("smaller.640.jpg", "JPEG") //另存为 var arr = imageTool.getBytes("JPEG") imageTool.swapSource() //添加水印: imageTool.loadAlpha("alpha.jpg") imageTool.loadMask("mask.jpg") imageTool.applyMask() //恢复原图: imageTool.restoreOriginal() 例子: def imageTool = new ImageTool() imageTool.load("/path/to/image.jpg") // Reduces the image size imageTool.thumbnail(640) // Saves the result imageTool.writeResult("image_smaller.jpg", "JPEG") // Crops it to a square imageTool.square() /* * Swaps the resulting image with the main one, so that we continue to * operate on the square crop. */ imageTool.swapSource() // Creates a copy of the original for later restore imageTool.saveOriginal() /* * Iterate through the thumbnail sizes that we want * */ [178, 133, 69].each { def tempName = "image.${it}.jpg" /* * Creates and saves the thumbnail. It needs to be temporarily saved * and re-loaded before applying the masking operation, because of * something that seems to be a JAI glitch. * See the following link for details: * http://ricardo.strangevistas.net/jai-and-masking-operations.html */ imageTool.thumbnail(it) imageTool.writeResult(tempName, "JPEG") imageTool.load(tempName) // Loads the alpha and mask, and applies them imageTool.loadAlpha("Alpha_${it}.jpg") imageTool.loadMask("Mask_${it}.jpg") imageTool.applyMask() // Finally, save it and restore the original so that we can continue // to operate on the unmodified image imageTool.writeResult(tempname, "JPEG") imageTool.restoreOriginal() }
include 插件
:
http://grails.org/plugin/include 让gsp支持包含标签
<inc:include url="/user/list"/> <inc:include url="/resource.html"/>
jcaptcha 验证码插件
:
http://grails.org/plugin/jcaptcha 验证码插件。以前一直在用。以前写过关于它的使用文章。或者参考其他的文章
http://romejiang.iteye.com/blog/212622
swfupload上传插件
:
http://grails.org/plugin/super-file-upload 使用js+flash(swfupload)的上传插件,但没有使用最新的swfupload。
有三个标签,首先使用generateConfiguration 配置标签。
<sfu:generateConfiguration fileSize="30" form="bookForm" buttonImageFile="/buttons/browse-button-sprite.png" buttonWidth="104" buttonHeight="30"/>
然后上传标签
<sfu:fileUploadControl/>
最后进度条标签
<sfu:fileUploadProgressBar/>
并在 form中加上 return sfuSubmitForm(this);
<form id="bookForm" name="bookForm" onsubmit="return sfuSubmitForm(this);">
acegi 安全插件
:
acegi : 安全、登陆、权限插件,使用的Spring Security,针对Spring Security进行了包装,让Spring Security更加简单好用。
http://grails.org/plugin/acegi 权限和验证框架。
http://www.infoq.com/cn/articles/grails-acegi-integration
http://www.grails.org/AcegiSecurity+Plugin+-+Customizing+with+SecurityConfig 配置文件解释
acegi插件infoq的文章已经讲的很明白了,我只说一些他没有涉及的一些东西。
authenticateService提供的一些方法
org.springframework.security.context.SecurityContextHolder.context.authentication.principal 获取已登陆的用户对象
authenticateService.principal() 只是获得一个当前登陆用的代理
authenticateService.userDomain() 这个才是返回当前登陆的用户域对象,但是需要注意这是登陆时存放在内存里的,在登陆后更新过的用户信息不会体现,所以应该注意在有时你需要: User.get(authenticateService.userDomain()?.id)
authenticateService.encodePassword(String passwd) acegi负责了用户注册和管理,密码都是加密过的,这个acegi所用的加密方法。
authenticateService.isLoggedIn() 判断是否登陆。
权限组
权限组前缀必须是ROLE_有人所可以不用,但我很早前试过好像不行,就没有尝试。
使用url设置权限时,最好低级的组权限高级的组
例如这样:
配置信息
defaultRole = 'ROLE_USER' 默认的权限组
使用acegi需要运行的生成命令:
grails create-auth-domains User Role Requestmap
grails generate-manager
grails generate-registration
quartz定时插件
:
grails install-plugin quartz // 安装
grails create-job
job-name // 生成job
class MyJob { static triggers = { simple name: 'mySimpleTrigger', startDelay: 60000, repeatInterval: 1000 } def group = "MyGroup" def execute(){ print "Job run!" } }
一些定时触发器规则的例子:
simple name:'simpleTrigger', startDelay:10000, repeatInterval: 30000, repeatCount: 10
cron name:'cronTrigger', startDelay:10000, cronExpression: '0/6 * 15 * * ?'
custom name:'customTrigger', triggerClass:MyTriggerClass, myParam:myValue, myAnotherParam:my
评论
发表评论
-
Calendar关于周相关的方法
2010-04-22 15:04 2150使用java.util.Calendar ... -
Grails从零开始开发项目三
2010-02-09 20:25 4756新建项目 准备工作做完了,开始创建项目,这里要说 ... -
Grails从零开始开发项目二
2010-02-08 18:05 2701下载,安装,配置Java,Groovy,Grails 肉 ... -
Grails从零开始开发项目一
2010-02-08 17:55 2192Java Java是一个让人爱恨难分的语言,很多人说 ... -
Google App Engine上运行Grails项目只需九步
2009-05-17 23:18 64625月14日Grails1.1.1发布,其中一个最振奋的特性就是 ... -
Integration test of Grails controller
2009-05-07 00:50 0http://sjtai.blogspot.com/2009/ ... -
在controllers和services以外的程序中访问config 和 metdata数据。
2009-05-07 00:28 0grailsApplication is injected ... -
Grails命令行的缩减版
2009-03-26 14:55 2288Grails命令的缩减版,简化了Grails命令的长度,都以首 ... -
为Grails 加上打包自定义排除文件功能。
2009-03-20 18:15 2063为Grails 加上打包自定义 ... -
Groovy新发布的 HTTPBuilder 模块
2008-12-18 11:10 8743http://groovy.codehaus.org/modu ... -
Groovy 闭包深入浅出
2008-07-15 10:18 20084闭包概念 Groovy语言中闭包(closure)是一个非常重 ... -
给Grails添加验证码插件
2008-07-08 10:23 6113在Grails安全验证插件分类中有几个和验证码相关的插件。Si ... -
Grails 现有插件的介绍
2008-07-08 10:19 3383本页面介绍了grails现今能用的插件连接。如果想自己开发插件 ...
相关推荐
NULL 博文链接:https://romejiang.iteye.com/blog/591632
Grails项目的应用越来越多,而对于初学者来说,在Eclipse下搭建Grails项目是一个难题,这个文档将教会你如何搭建Grails项目,希望对你有所帮助。
Grails企业web应用开发与部署.pdf 很好的资源
Grails_开发简介,本文介绍如何使用Oracle JDeveloper 作为IDE 来开发Grails 应用程序。此外,您还在学习开发Grails 应用程序的过程中大概了解Groovy 的一些概念。
这是一个在Eclipse下导入Grails项目,并对环境和报错进行相应调试的文档
详细讲解grails开发环境配置。 详细讲解grails连接mysql数据库,crud开发
grails开发关于grails入门指南的例子进行分析
grails1.0开发框架4 类似于ruby on rails的框架。
grails开发实践,欢迎下载使用 grails开发实践,欢迎下载使用 grails开发实践,欢迎下载使用
grails1.0开发框架1 类似于ruby on rails的框架。
关于grails企业web开发环境和部署的一些经验,和大家分享。
grails快速开发web.pdf
grails 用户登录的例子,必须登录后才可以查看信息,并且只有admin用户才可以创建用户,此例子,只要将grails 的环境配好,就可以用了,用的是自带的数据库,如果需要mysql 可以更改。
自己买的书,然后用扫描机扫描的,整个文件太大了,不能一次性上传上来,所以拆成3个part。 我自己学grails很想看这本书,结果网上没有,就自己去买了,然后共享给需要的人。 如果有什么问题请联系我下架。
自己买的书,然后用扫描机扫描的,整个文件太大了,不能一次性上传上来,所以拆成3个part。...我自己学grails很想看这本书,结果网上没有,就自己去买了,然后共享给需要的人。 如果有什么问题请联系我下架。
Apress.Beginning.Groovy.and.Grails.From.Novice.to.Professional.Jun.2008 grails_programming Grails1.1中文文档-----2009.3.25 Grails入门指南(第二版) Groovy经典入门 Programming Groovy 2 合集,超实惠哦
Grails Grails Grails Grails Grails
使用 Grails 和 jQuery 快速开发一个 Web Calendar 程序。
本教程介绍 Grails,这是一个搭建在动态语言 Groovy 之上的开源 MVC 快速 Web 开发框架。使用 Grails 可以提高 Web 开发的...从 Grails 自动生成代码入手,以示例为中心逐步增加深度与难度,对 Grails 开发进 行演示。
grails jobs 定时任务 项目demo