`
romejiang
  • 浏览: 95455 次
  • 性别: Icon_minigender_1
  • 来自: ...
社区版块
存档分类
最新评论

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

0
0
分享到:
评论
2 楼 fenglin_Java 2012-04-06  
能问个问题吗?能大概说下你做项目的时候注册是怎么做的吗?验证怎么验证的呢?用插件做的,还是什么呢?我本来是想用jquery做的,后来发现grails自带验证功能,我就用grails做了,但是做着做着发现很多情况不好实现,现在做的没头绪了。能请教一下你是怎么做的吗?
1 楼 wenxiang_tune 2011-09-17  
谢谢,灰常不错,我将继续关注。。。。

相关推荐

Global site tag (gtag.js) - Google Analytics