Groovy 学习笔记 (五)
(1..10).each{ log += it } 等价于 (1..10).each({ log += it })
Closure声明方式之一是: reference.& (其中receiver是对象)
def adder = { x, y -> return x+y } 等价于 adder(4, 3) 或 adder.call(2, 6)
文件读写更加简单:
def file = new File(‘myFileName.txt’)
for (line in file) println line
方法的最后一个语句得到的值作为返回值,所以return语句是可选的
异常不强制要求捕获
for (it in aaa) 中,变量 aaa 可以是字符串,可以是对象,等等
def file = new File(‘myFileName.txt’)
for (line in file) println def file = new [...]
Categorized in: Scripts · Tagged with: grails, Groovy, Scripts
Groovy 学习笔记 (四)
Groovy 支持操作符重载, 每个操作符对应一个方法签名, 如‘+’对应的是’plus’.
Groovy 支持多种字符串表示, 单引号, 双引号(支持GString), 3个单引号(支持多行), 3个双引号(支持多行, 支持GString).
在 Groovy 中, 方法的括号是可选的, 如果一行只有一个语句, 那么语句后的分号也可省略不写.
Groovy range, 可用于: 数字, 日期, 字符串. 只要数据类型实现了 next(++), previous(–), 和 java.lang.Comparable 接口, 就可以使用 range .
Groovy list 默认使用 ArrayList, 欲使用 LinkedList 等其它类型 List, 需要明确声明.
List 可以使用负值作为index值进行访问, 例如 list[-1]返回的就是list的最后一个值, list[-2]则返回倒数第二个值. 也可以指定倒序的 range, 如list[4..0].
需注意: list[0..<-2] 等价于 list[0..-1] 而非 list[0..-3].
声明空的map: [:]
['a':1] 等价于 [a:1]
Categorized in: Scripts · Tagged with: Groovy, Java, Scripts
Groovy 学习笔记 (三)
1. == 是判断是否 equals, 而判断是否是同一对象则用 is
2. list 和 map 的一些方法:
def x = 1..10
assert x.contains(5)
assert x.contains(15) == false
assert x.size() == 10
assert x.from == 1
assert x.to == 10
assert x.reverse() == 10..1
3. 以下对象都有 size() 方法
Array, String, StringBuffer, Collection, Map, File
4. 数据类型:12 – Integer, 100L – Long, 1.23F – Float, 1.23D – Double, 123G – BigInteger, 1.23,1.23G – BigDecimal
如果一个小数, [...]
Categorized in: Scripts · Tagged with: Groovy, Java, Scripts
How to set onSuccess event listener for Grails
Grails inherently supports ajax function, such as remoteFunction, remoteLink, etc.
But do you know how to set onSuccess event listener appropriately?
Let me tell you
<input type=”text” id=”name” name=”name” value=””/>
<input type=”button” name=”checkbizname” value=”Chek whether this name already exists” onclick=”${remoteFunction(controller:’mycontroller’,action:’isNameExists’,onSuccess:’popwin(o)’, params:’\’name=\’ + document.getElementById(\’name\’).value’)}”/>
In the above code, when ajax request is handled and get response from server, onSuccess [...]
Categorized in: Scripts · Tagged with: grails, Groovy, Java, Javascript
Groovy 学习笔记 (二)
5. 方法调用: 方法调用时, 括号是可选的. 例如: print ‘hello, world’
6. 方法返回值: 默认最后一句就是方法的返回值, 也就是说, 最后一句的return语句是可有可无的. 当然, 如果在方法中间需要return值, 还是要写 return 语句才行.
7. 闭包(closure): 闭包可以访问与闭包定义在同一scope的变量. 例如:
def name = “” //initialize variable
def printName = { println “The string in the name variable is ” + name } //define method
name = “Youssef” //set string Youssef in variable name
printName() //result: The string in the [...]
Categorized in: Scripts · Tagged with: grails, Groovy, Java, Scripts
Grails RichUI plugin
Rich UI 是一个 AJAx 插件, 可以干很多事情:
#Accordion
#AutoComplete
#CalendarDayView
#CalendarMonthView
#CalendarWeekView
#Carousel
#CheckedTreeView
#DateChooser
#Flow
#Font (creates images dynamically)
#Map
#Portlet
#Redirect
#ReflectionImage
#RichTextEditor
#Star rating
#TabView
#TagCloud
#Timeline
#Tooltip
#TreeView
我就用它做了一个 rating 功能, 哈哈, 真是超级简单, 超级方便. 在下面的链接里, 有详细的使用说明, 只要你的英语不是很烂, 就很容易搞明白如何使用.
具体地址: RichUI Plugin
Categorized in: Scripts · Tagged with: AJAX, grails, Groovy, Javascript, Plugins
Groovy 学习笔记 (一)
1. 关于字符串:
简单字符串可以用单引号和双引号, 但如果使用GString, 则必须使用双引号. 比如 “$foo, hello world”
多行字符串则可以使用 “”” (3个双引号), 例如:
def text = “””\
hello there ${name}
how are you today?
“””
如果对这个text进行输出,会发现输出是按原样式输出的,即换行符也输出. 这在处理类似 HTML 代码时时特别有用的.
另外, 可以使用 /…/ 来定义字符串, 如: def basename = /[Strings and GString^\/]+$/
在这种情况下, 只需对 / 进行转义: \/; 但如下定义是不合法的:def x = //, 可以这么写: def x = /${}/
2.字符串操作:
Categorized in: Scripts · Tagged with: grails, Groovy
Grails – JCaptcha Plugin usage
JCaptcha
JCaptcha 是一个开源 (LGPL) captcha 解决方案. JCaptcha 提供了图片的和音频的方式, 同时只要你愿意, 也可以进行扩展从而提供其它不同的方式.
支持版本
||Version||Minimum Grails Version||Notes|| | 0.1 | 0.6 | | | 0.2 | 1.0-RC1 | |
Plugin
Grails JCaptcha plugin 提供了一种简易方式去定义 Captchas, 显示和验证.
安装
该插件存放于官方库, 所以可以通过如下方式进行安装
grails install-plugin jcaptcha
用法
定义 Captchas
Captchas 定义在 grails-app/conf/Config.groovy 文件中.
log4j {
/* log4j config */
}
jcaptchas { captcha1 = … captcha2 = … }
每个 jcaptcha.* 条目必须是一个 CaptchaService 的实例, 负责生成验证方式和进行验证.
一些例子 Captchas …
import java.awt.Font
import [...]

(
(4.00 out of 5)