Grails 1.1 Beta 2發佈
2008 年 12 月 23日
SpringSource 和Grails 開發小組非常高興的宣佈Grails web application 開發框架的 1.1 beta 2 版本發佈了.
Grails 是一個構建在Java 和Groovy 之上的動態web application framework ,利用Java EE 領域的最好的breed APIs 包括Spring, Hibernate 和 SiteMesh 。允許Java developers 利用他們使用多年的已有知識和被證實配置複雜的經歷, Grails 給Java and Groovy 開發者帶來了基於約定快速開發的樂趣。
關於此次發佈的更多信息,請訪問下面的鏈接:
- Changelog: http://jira.codehaus.org/browse/GRAILS?report=com.atlassian.jira.plugin.system.project:changelog-panel
- 下載: http://grails.org/Download
- 文檔: http://grails.org/doc/1.1
下面介紹一下1.1 版本的新特性.
更好的GORM 事件 (Better GORM events )
之前, GORM 只支持 beforeInsert, beforeUpdate 和beforeDelete 事件, 現增加了afterInsert, afterUpdate 和afterDelete 來完成圖片操作
Persistence of Collections of Basic Types 基本類型集的持久化
GORM 現在支持基本類型比如String, Integer 等使用一個連接表的持久化。
class Person {
static hasMany = [nicknames:String]
}
對象的只讀訪問Read-Only Access to Objects
現在,持久化實例對象可以使用read 方法以只讀狀態被加載:
def book = Book.read(1)
現在,關聯可以使用一個類級別聲明的默認的排列順序來排序:
class Book {
String title
static mapping = {
sort "title"
}
}
或在關聯級別上:
class Author {
static hasMany = [books:Book]
static mapping = {
books sort:"title"
}
}
現在GORM 支持使用ORM DSL 在類級別上配置批處理 (batch fetching )( 延遲加載的優化):
class Book {
String title
static mapping = {
batchSize 15
}
}
或在關聯級別上:
class Author {
static hasMany = [books:Book]
static mapping = {
books batchSize:15
}
}
動態 Finders 的改進Improvements to Dynamic Finders
動態查詢器的新後綴InList 可用:
def groovyBooks = Book.findByAuthorInList(['Dierk Koenig', 'Graeme Rocher'])
現在,Dynamic finders 也能查詢緩存:
def books = Book.findByTitle("Groovy in Action", [cache:true] )
可以使用悲觀鎖:
def books = Book.findByTitle("Groovy in Action", [lock:true] )
單項的One-to-manys 遺留映射Legacy Mapping for Unidirectional
單項的One-to-manys 關聯關係可以使用joinTable 參數改變它們對底層數據庫的映射:
class Book {
String title
static belongsTo = Author
static hasMany = [authors:Author]
static mapping = {
authors joinTable :[name:"mm_author_books", key:'mm_book_id' ]
}
}
class Author {
String name
static hasMany = [books:Book]
static mapping = {
books joinTable:[name:"mm_author_books", key:'mm_author_id']
}
}
增強枚舉類型的支持Enhanced Enum Support
現在,枚舉類型使用GORM 調用的getId ()方法來持久化枚舉狀態。
enum Country {
AUSTRIA('at'),
UNITED_STATES('us'),
GERMANY('de');
final String id
Country(String id) { this.id = id }
}
現在,安裝插件可以給所有的應用程序共享:
grails install-plugin webtest -global
多插件倉庫Multiple Plugin Repositories
現在,Grails 支持通過提供多插件倉庫配置的能力
使用USER_HOME/.grails/settings.groovy 文件或包含配置好的倉庫詳情的grails-app/conf/BuildConfig.groovy 文件。
grails.plugin.repos.discovery.myRepository="http://svn.codehaus.org/grails/trunk/grails-test-plugin-repo"
grails.plugin.repos.distribution.myRepository="https://svn.codehaus.org/grails/trunk/grails-test-plugin-repo"
The Grails plugin discovery commands like list-plugin and install-plugin will then automatically work against all configured repositories. To release a plugin to a specific repository you can use the repository argument:
grails release-plugin -repository=myRepository
自動安裝插件方案 Automatic Transitive Plugin Resolution
插件不再需要到SVN 檢出,當應用程序第一次加載時,通過插件元數據會自動安裝。
另外, plugin dependencies are now resolved transitively.
插件的作用範圍和環境 Plugin Scopes and Environments
現在,插件可以作用於環境或預置的構建範圍內:
def environments = ['dev', 'test']
def scopes = [excludes:'war']
僅在那些環境中加載使用,而不打包到WAR 文件中。這使得產品使用時 "development-only" 的插件不會被打包 。
現在,作爲1.0.x 系列可用插件的新測試框架 已集成到 Grails 1.1.
該測試框架增加了模擬所以普通類型包擴控制器,領域類,標籤庫和url 映射簡寫的行爲,快速運行單元測試。
class SongTests extends grails.test.GrailsUnitTestCase {
void testMinimumDuration() {
mockDomain(Song)
def song = new Song(duration: 0)
assertFalse 'validation should have failed', song.validate()
assertEquals "min", song.errors.duration
}
}
屬性子集的數據綁定Data Binding a Subset of Properties
It is now simpler to bind data to a subset of properties. Previously you could use the syntax:
person.properties = params
Which would bind all the incoming request parameters to the person. If you didn't want that behavior you could use the bindData method. Now you can bind to a subset of properties using the subscript operator:
person.properties["firstName","lastName"] = params
And access a subset of the domain classes properties using the same syntax:
person.properties["firstName","lastName"].each { println it }
集合類型的數據綁定Data Binding for Collection Types
Grails now supports data binding to collections types including lists, sets and maps.
<g:textField name="books[0].title" value="the Stand" />
<g:textField name="books[1].title" value="the Shining" />
<g:textField name="books[2].title" value="Red Madder" />
Grails will auto-instantiate domain instances at the given indices and the necessary values will be populated in the association.
模板和動態腳手架Templates and Dynamic Scaffolding
現在,動態腳手架使用通過install-templates 命令安裝的模板 。
支持更多關聯類型Support for more association types
現在,Scaffolding 支持 many-to-many 和單項的 one-to-many 關聯.
在JSP 中支持JSP 標籤庫 JSP Tag library support in JSP
現在,GSP 已經支持JSP 標籤庫複用的能力:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:formatNumber value="${10}" pattern=".00"/>
JSP 標籤也可以像正常的GSP 標籤一樣調用:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
${fmt.formatNumber(value:10, pattern:".00")}
工程基本結構(Project Infrastructure )
Grails 1.1 緣自和 Maven plugin 和archetype 的關聯,允許你使用Maven 更容易的構建Grails 工程。根據操作指南 here 或使用原型來創建一個新的Grails 工程, 或運行:
mvn grails:create-pom
來爲已有工程創建一個Maven POM.
環境及元數據API Environment and Metadata API
使用新 API 來訪問當前環境:
import grails.util.Environment
...
switch(Environment.current) {
case Environment.DEVELOPMENT:
configureForDevelopment()
break
case Environment.PRODUCTION:
configureForProduction()
break
}
當然也有一個易於訪問應用程序元數據的新類:
def metadata = grails.util.Metadata.current
println metadata.applicationName
println metadata.applicationVersion
新的 Log4j DSL 用於替換以前Log4j 配置的方式:
log4j = {
error 'org.codehaus.groovy.grails.web.servlet', // controllers
'org.codehaus.groovy.grails.web.pages' // GSP
warn 'org.mortbay.log'
}
詳見user guide 中Log4j DSL 全部文檔。
靈活的構建配置 Flexible Build Configuration
新的 grails-app/conf/BuildConfig.groovy 文件可用,它允許你配置不同層面的Grails 構建輸出路徑和服務器使用插件的解決方案:
grails.work.dir="/tmp/work"
grails.plugins.dir="/usr/local/grails/plugins"
grails.project.test.reports.dir="/usr/local/grails/test-reports"
現在,Grails 支持一種--non-interactive flag ,須鍵入到命令行,目的是關閉用戶提示:
grails run-app --non-interactive
加密數據源 Encrypted Data Sources
現在,數據源密碼可以使用已提供的編碼類來加密:
dataSource {
username = "foo"
password = "438uodf9s872398783r"
passwordEncryptionCodec="my.company.encryption.BlowfishCodec"
}
支持的編碼使用 Grails' 現存的 編碼機制
Grails 1.1 有很多改變,但大多是向後兼容1.0.x 系列的。如果有問題請報告。升級時,以下是已知需要注意的問題列表:
- Plugins 不保存在你的 USER_HOME 路徑下. 你需要重寫安裝插件或運行:
grails -Dgrails.plugins.dir=./plugins run-app
- 現在枚舉類型已經被映射到數據庫,使用字符串值而不是原始默認的。
- jsession id 默認已無效. 詳見 GRAILS-3364
- GSP 空白符處理已經變好很多了,比以前有更多空白符. 詳見 GRAILS-3277
- grails.testing.reports.destDir 配置選項已被替代爲grails.project.test.reports.dir
- 現在,PreInit.groovy 改爲BuildConfig.groovy
- 控制器中的allowedMethod 屬性被標識爲static 。非 static 版本不推薦使用,儘管它仍然起作用並在控制檯產生信息。