Grails

来自开放百科 - 灰狐
(版本间的差异)
跳转到: 导航, 搜索
(相关链接)
 
(未显示1个用户的62个中间版本)
第1行: 第1行:
 +
{{top news}}
 +
{{SeeWikipedia|Grails (framework)}}
 
[[Image:grails.jpg|right]]
 
[[Image:grails.jpg|right]]
 +
 
Grails关注将"规约编程"(coding by convention)引入[[Groovy]]。它是一个开放源网络应用构架, 对Groovy语言起到杠杆作用,并且对Java网络开发起到补足作用。你可以用Grails 作为独立开发环境, 它隐藏了所有的配置细节,也可以将它同和入你的Java商务逻辑。
 
Grails关注将"规约编程"(coding by convention)引入[[Groovy]]。它是一个开放源网络应用构架, 对Groovy语言起到杠杆作用,并且对Java网络开发起到补足作用。你可以用Grails 作为独立开发环境, 它隐藏了所有的配置细节,也可以将它同和入你的Java商务逻辑。
  
第5行: 第8行:
  
 
Grails构建在开源技术如[[Spring]]、[[Hibernate]]和[[SiteMesh]]之上,提供了一个类似于[[Rails]] 的平台可以与Java平台无缝集成. [[Groovy]]是一种“动态”或指令语言。动态语言一般用来加速和简化程序撰写。与Python或Ruby等其他指令语言不同的是,用Groovy撰写的程序,可与Java虚拟器结合。
 
Grails构建在开源技术如[[Spring]]、[[Hibernate]]和[[SiteMesh]]之上,提供了一个类似于[[Rails]] 的平台可以与Java平台无缝集成. [[Groovy]]是一种“动态”或指令语言。动态语言一般用来加速和简化程序撰写。与Python或Ruby等其他指令语言不同的是,用Groovy撰写的程序,可与Java虚拟器结合。
 +
==News==
 +
*[http://www.infoq.com/cn/news/2008/11/springsource-g2one SpringSource宣布收购G2One,正式介入Groovy和Grails领域] [[Image:springsource-acquires-g2one.png|right]]
 +
==安装==
 +
通过[[sdkman]]安装Grails
 +
sdk install grails
 +
sdk upgrade grails
  
==Install==
+
通过[[Homebrew]]安装Grails
  wget http://download.huihoo.com/grails/grails-bin-1.0.1.zip
+
  brew install grails
 +
brew upgrade grails
 +
其它方式
 +
[http://grails.org/Download 下载],如:wget http://download.huihoo.com/grails/grails-bin-1.0.1.zip
 
  GRAILS_HOME= C:\grails-1.0.1 or /usr/local/grails-1.0.1  
 
  GRAILS_HOME= C:\grails-1.0.1 or /usr/local/grails-1.0.1  
 
  Add the "%GRAILS_HOME%\bin" directory to your PATH environment variable
 
  Add the "%GRAILS_HOME%\bin" directory to your PATH environment variable
 
  Add the "%GRAILS_HOME%\ant\bin directory to your PATH environment variable.
 
  Add the "%GRAILS_HOME%\ant\bin directory to your PATH environment variable.
   
+
 
  grails create-app
+
==指南==
  input project name: my-project
+
  grails create-app helloworld
 +
cd helloworld
 +
  grails create-domain-class book
 +
grails generate-all helloworld.Book
 +
grails -Dserver.port=9000 run-app
 +
  ./grailsw compile
 +
./grailsw -Dreindex=1 run-app
 +
./grailsw -Dserver.port=9000 run-app
 +
http://localhost:9000
 +
 +
grails create-app my-project
 
  cd my-project
 
  cd my-project
  grails create-domain-class
+
  grails create-domain-class Book
input domain name: Book
+
 
my-project\grails-app\domain\Book.groovy
 
my-project\grails-app\domain\Book.groovy
 
  class Book {
 
  class Book {
第37行: 第58行:
 
     def scaffold = Book
 
     def scaffold = Book
 
  }
 
  }
  grails run-app
+
  grails run-app // or grails dev run-app or grails prod run-app
 
  http://localhost:8080/my-project/
 
  http://localhost:8080/my-project/
 
  http://localhost:8080/my-project/book
 
  http://localhost:8080/my-project/book
第44行: 第65行:
 
  grails clean
 
  grails clean
 
  grails upgrade
 
  grails upgrade
 +
 
==NetBeans==
 
==NetBeans==
 
http://wiki.netbeans.org/groovy
 
http://wiki.netbeans.org/groovy
 +
==PostgreSQL==
 +
[[Grails and PostgreSQL]]
 +
 +
下载例子: http://docs.huihoo.com/postgresql/regperf.zip
 +
 +
More Detail: http://blogs.sun.com/tomdaly/entry/regperf_v1_running_the_grails
 +
==CRUD==
 +
Create
 +
def p = new Person(name:"Jack", age:32, lastVisit:new Date())
 +
p.save()
 +
Read
 +
def p = Person.get(1)
 +
assert 1 == p.id
 +
Update
 +
def p = Person.get(1)
 +
p.name = "Peter"
 +
p.save()
 +
Delete
 +
def p = Person.get(1)
 +
p.delete()
 +
==GORM==
 +
One-to-one
 +
class Face {
 +
    Nose nose
 +
}
 +
class Nose {
 +
static belongsTo = [face:Face]
 +
}
 +
new Face(nose:new Nose()).save() // ok
 +
 +
def f = Face.get(1)
 +
f.delete() // both Face and Nose deleted
 +
One-to-many
 +
class Author {
 +
    static hasMany = [ books : Book ]
 +
    String name
 +
}
 +
class Book {
 +
    static belongsTo = [author:Author]
 +
    String title
 +
}
 +
Many-to-many
 +
class Book {
 +
  static belongsTo = Author
 +
  static hasMany = [authors:Author]
 +
  String title
 +
}
 +
class Author {
 +
  static hasMany = [books:Book]
 +
  String name
 +
}
 +
 +
Grails 1.0.3 Scaffolding feature does not currently support many-to-many relationship and hence you must write the code to manage the relationship yourself
 +
 +
[http://reverttoconsole.com/archives/194 Grails ManyToMany GORM Example]
 +
 +
GPath: Groovy's ability to manipulate collections via GPath and methods like sort, findAll and so on combined with GORM results in a powerful combination.
 +
def books = Book.list()
 +
def books = Book.list(offset:10, max:20)
 +
def books = Book.list(sort:"title", order:"asc")
 +
def book = Book.get(23)
 +
def books = Book.getAll(23, 93, 81)
 +
def book = Book.findByTitle("The Stand")
 
==Usage==
 
==Usage==
 
grails help
 
grails help
第85行: 第170行:
 
*grails upgrade
 
*grails upgrade
 
*grails war
 
*grails war
 +
==Service==
 +
Services in Grails are seen as the place to put the majority of the logic in your application, leaving controllers responsible for handling request flow via redirects and so on.
 +
grails create-service simple
 
==Plugins==
 
==Plugins==
 
  grails list-plugins
 
  grails list-plugins
Plug-ins available in the Grails repository are listed below:
 
*acegi              <no releases>    --  No description available
 
*aop                <no releases>    --  No description available
 
*autorest            <no releases>    --  No description available
 
*axis2              <0.1>            --  Add Web Service support for Grails services using Apaxhe Axis2.
 
*cache              <no releases>    --  No description available
 
*cas-client          <0.2>            --  This plugin provides client integration for JA-SIG CAS
 
*code-coverage      <0.7>            --  Generates Code Coverage reports
 
*converters          <0.3>            --  Provides JSON and XML Conversion for common Objects (Domain Classes, Lists, Maps, POJO)
 
*dbmapper            <0.1.7>          --  Database relation to domain object generator
 
*dbmigrate          <0.1.5>          --  Database migration tasks
 
*djangotemplates    <no releases>    --  No description available
 
*dojo                <0.4.3>          --  Provides integration with the Dojo toolkit http://dojotoolkit.org, an Ajax framework.
 
*dwr                <0.1>            --  This plugin adds DWR capabilies to the services in a Grails application.
 
*eastwood-chart      <0.1>            --  This plugin wraps JFreeChart's Eastwood servlet for Grails app
 
*echo2              <0.1>            --  Echo2 capabilities to Grails applications
 
*ext-ui              <no releases>    --  No description available
 
*fck-editor          <0.3>            --  FCKeditor
 
*feeds              <1.0>            --  Render RSS/Atom feeds with a simple builder
 
*flex                <0.1>            --  Provides integration between Grails and Flex
 
*google-chart        <0.4.3>          --  This plugin adds Google Chart API features to Grails applications.
 
*gwt                <0.2.4>          --  The Google Web Toolkit for Grails.
 
*i18n-templates      <1.0.1>          --  I18n Templates
 
*ivy                <0.1>            --  The Ivy Dependency Manager for Grails.
 
*jasper              <0.5>            -- 
 
*jcaptcha            <0.2>            --  Grails JCaptcha Plugin
 
*jcr                <no releases>    --  No description available
 
*jms                <0.3>            --  This plugin adds MDB functionality to services.
 
*jsecurity          <0.1.1>          --  Security support via the JSecurity framework.
 
*jsf                <no releases>    --  No description available
 
*laszlo              <0.6>            --  OpenLaszlo plugin for Grails
 
*ldap                <0.1>            --  Adds easy to use LDAP connectivity
 
*liquibase          <1.5.2.0>        --  LiquiBase Database Refactoring for Grails
 
*modalbox            <0.1>            --  This plugin adds the ModalBox to your Grails applications.
 
*mondrian            <0.1>            --  This plugin installs Pentaho Mondrian into your Grails application.
 
*ofchart            <0.5>            -- 
 
*portlets            <no releases>    --  No description available
 
*quartz              <0.2 (?)>       --  This plugin adds Quartz job scheduling features to Grails application.
 
*radeox              <0.1>            --  A plugin for the Radeox Wiki-render engine to allow easy markup and cross-linking.
 
*remoting            <0.3>            --  Adds easy-to-use server-side and client-side RPC support.
 
*richui              <0.2>            --  Provides a set of AJAX components
 
*scaffold-tags      <0.7>            --  Adds tags to support fully-customizable & dynamic scaffolding
 
*searchable          <0.4-SNAPSHOT>  --  Adds rich search functionality to Grails domain models.
 
*staticresources    <no releases>    --  No description available
 
*webtest            <0.3>            --  A plug-in that provides functional testing for Grails using Canoo Web Test
 
*wicket              <0.3>            --  Provides integration between Grails and the Wicket framework
 
*xfire              <0.7.4>          --  Add Web Service support for Grails services using XFire.
 
*xmlrpc              <0.1>            --  This plugin adds XML RPC functionality to GRAILS
 
*yui                <2.5.0>          --  Yahoo! User Interface Library (YUI)
 
*yui-widgets        <0.1>            --  Yahoo! User Interface Library (YUI) Widgets
 
 
 
To find more info about plugin type 'grails plugin-info [NAME]'
 
To find more info about plugin type 'grails plugin-info [NAME]'
  
第144行: 第181行:
  
 
For further info visit http://grails.org/Plugins
 
For further info visit http://grails.org/Plugins
 +
==GSP==
 +
<html>
 +
    <body>
 +
    <%-- This is my comment --%>  //  <%="Hello GSP!" %>  JSP-style comment
 +
    <%="Hello GSP!" %>
 +
    </body>
 +
</html>
 +
<%@ page import="java.awt.*" %>
 +
<%@ page contentType="text/json" %>
 +
GSP Tags
 +
<g:example attr="${new Date()}" attr2="[one:'one', two:'two']">
 +
    Hello world
 +
</g:example>
 
==Ext==
 
==Ext==
 +
[[Image:grails-ext-screen.gif|right|thumb|Grails Ext Userinterface]]
 
  grails install-plugin ext or grails install-plugin ext 2.0.2
 
  grails install-plugin ext or grails install-plugin ext 2.0.2
 
  Ext has installed under your project's /web-app/js/ext/2.0.2
 
  Ext has installed under your project's /web-app/js/ext/2.0.2
第150行: 第201行:
  
 
[http://grails.org/Using+ExtJS+Layouts+with+Grails+Layouts Using ExtJS Layouts with Grails Layouts]
 
[http://grails.org/Using+ExtJS+Layouts+with+Grails+Layouts Using ExtJS Layouts with Grails Layouts]
 +
 +
[http://marceloverdijk.blogspot.com/2008/04/update-on-ext-scaffolding.html Update on Ext Scaffolding]
 +
grails install-templates // when finished the Ext plugin will contain a plugin to install these templates
 +
grails generate-all
 +
 
==GWT==
 
==GWT==
 
  grails install-plugin gwt
 
  grails install-plugin gwt
第158行: 第214行:
 
  grails run-gwt-client
 
  grails run-gwt-client
 
More Detail: http://www.grails.org/GWT+Plugin  
 
More Detail: http://www.grails.org/GWT+Plugin  
 +
 +
GWTGrailsTutorial: http://code.google.com/p/derjanandhisblog/wiki/GWTGrailsTutorial   
 +
==JSON==
 +
def list = {
 +
def results = Book.list()
 +
render(contentType:"text/json") {
 +
books {
 +
for(b in results) {
 +
book(title:b.title)
 +
}
 +
}
 +
}
 +
}
 +
Result
 +
[
 +
{title:"ActiveRecord Tutorial"},
 +
{title:"Grails Tutorial"}
 +
]
 +
==Web Flow==
 +
[[Image:spring-webflow2-distribution.png|right|thumb|Spring Web Flow]]
 +
Grails supports the creation of web flows built on the [http://www.springframework.org/webflow Spring Web Flow project].
 +
class BookController {
 +
    …
 +
    def shoppingCartFlow = {
 +
        showCart {
 +
            on("checkout").to "enterPersonalDetails"         
 +
            on("continueShopping").to "displayCatalogue"
 +
        }
 +
        …
 +
        displayCatalogue {
 +
            redirect(controller:"catalogue", action:"show")
 +
        }
 +
        displayInvoice()
 +
    }
 +
}
 +
Web flows don't require an HTTP session, but instead store their state in a serialized form, which is then restored using a flow execution key that Grails passes around as a request parameter. This makes flows far more scalable than other forms of stateful application that use the HttpSession and its inherit memory and clustering concerns.
 +
 +
[[GrailsFlow]] is a Workflow and Business Process Engine written in Groovy and Grails.
 +
==Rules==
 +
*[[GroovyRules]]
 +
==Tutorials==
 +
[http://www.devx.com/Java/Article/37487 Build Your First Grails Project: A Grails-Powered Blog]
 +
==NetBeans==
 +
Groovy and Grails 在开发版中已经可用 http://wiki.netbeans.org/groovy
 +
 +
开发版: http://deadlock.netbeans.org/hudson/job/trunk/lastSuccessfulBuild/artifact/nbbuild/dist/zip/
 +
==REST==
 +
As an example the following mappings provide a RESTful API URL mappings for the BookController:
 +
static mappings = {
 +
    "/product/$id"(controller:"product"){
 +
        action = [GET:"show", PUT:"update", DELETE:"delete", POST:"save"]
 +
    }
 +
}
 +
==SOAP==
 +
Grails supports SOAP through the [[XFire]] plug-in which uses the popular XFire SOAP stack to integrate SOAP support into Grails.
 +
class BookService {
 +
  static expose=['xfire']
 +
  Book[] getBooks()
 +
  { Book.list() as Book[]
 +
} }
 +
http://127.0.0.1:8080/your_grails_app/services/book?wsdl
 +
==Ajax==
 +
===Ajax with GWT===
 +
===Ajax with Prototype===
 +
===Ajax with Dojo===
 +
==Validation==
 +
class User {
 +
    String login
 +
    String password
 +
    String email
 +
    Integer age
 +
 +
    static constraints = {
 +
        login(size:5..15, blank:false, unique:true)
 +
        password(size:5..15, blank:false)
 +
        email(email:true, blank:false)
 +
        age(min:18, nullable:false)
 +
    }
 +
}
 +
==Testing==
 +
grails test-app SimpleController BookController
 +
===Unit Testing===
 +
===Integration Testing===
 +
===Functional Testing===
 +
Grials support for [http://webtest.canoo.com/webtest/manual/WebTestHome.html Canoo WebTest] via a plug-in.
 +
[[Image:TestResult-GoogleWebTestTop.png|right|thumb|Canoo WebTest]]
 +
grails install-plugin webtest
 +
==Internationalization==
 +
grails-app/i18n
 +
messages.properties
 +
messages_zh.properties
 +
messages_de.properties
 +
messages_es.properties
 +
etc.
 +
==Security==
 +
class SecurityFilters {
 +
  def filters = {
 +
      loginCheck(controller:'*', action:'*') {
 +
          before = {
 +
              if(!session.user && actionName != "login") {
 +
                  redirect(controller:"user",action:"login")
 +
                  return false
 +
}} } } }
 +
===Acegi===
 +
[http://vancouverlinuxguy.com/blog/2008/06/05/grails-acegisecurity-create-user-on-bootstrap/ Grails AcegiSecurity Create User on BootStrap]
 +
===JSecurity===
 +
class ExampleController extends JsecAuthBase {
 +
    static accessControl = {
 +
        // All actions require the 'Observer' role.
 +
        role(name: 'Observer')
 +
        // The 'edit' action requires the 'Administrator'
 +
        role. role(name: 'Administrator', action: 'edit')
 +
        // Alternatively, several actions can be specified.
 +
        role(name: 'Administrator', only: [ 'create', 'edit', 'save', 'update' ]) }
 +
… }
 +
==RSS and Atom==
 +
[http://docs.codehaus.org/display/GRAILS/Feeds+Plugin Feeds plug-in] available for Grails that provides a RSS and Atom builder using the popular [https://rome.dev.java.net/ ROME] library.
 +
def feed = {
 +
    render(feedType:"rss", feedVersion:"2.0") {
 +
        title = "My test feed"
 +
        link = "http://your.test.server/yourController/feed"
 +
 +
        Article.list().each() {
 +
            entry(it.title) {
 +
                link = "http://your.test.server/article/${it.id}"
 +
                it.content // return the content
 +
} } } }
 +
==Grails and Spring==
 +
==Grails and Hibernate==
 +
==Scaffolding==
 +
class BookController {
 +
    def scaffold = Book
 +
    def changeAuthor = {
 +
        def b = Book.get( params["id"] )
 +
        b.author = Author.get( params["author.id"] )
 +
        b.save()
 +
        redirect(action:show)
 +
} }
 +
==Template==
 +
* SimpleTemplateEngine – for basic templates
 +
* GStringTemplateEngine – stores the template as writable closures (useful for streaming scenarios)
 +
* XmlTemplateEngine – works well when the template and output are valid XM
 +
==Deployment==
 +
grails war /glassfish-v2ur2/domains/domain1/applications/j2ee-modules/blog.war
 +
==Mobile==
 +
[[Image:grails-iphone-mlist.jpg|right|thumb|Grails on iPhone]]
 +
*[http://www.ibm.com/developerworks/java/library/j-grails06178/ Grails and the mobile Web]
 +
*[http://www.ibm.com/developerworks/cn/java/j-grails06178/ Grails 与移动 Web]
 
==相关项目==
 
==相关项目==
 +
*[[OpenSymbolic]]
 
*[[Scala]]   
 
*[[Scala]]   
==相关链接==
+
==Samples==
*http://grails.codehaus.org/
+
http://svn.codehaus.org/grails/trunk/grails-samples/
 +
==成功应用==
 +
*http://grails.org/
 +
*http://www.grassr.com/
 +
*http://feedlr.com Feedlr, 创建你自己的Feed广播机器人
 +
http://www.grails.org/Success+Stories
 +
*[https://wiki.sdn.sap.com/wiki/display/Community/Composition+On+Grails SAP Composition On Grails]
 +
==图集==
 +
<gallery>
 +
image:groovy-grails-architecture.jpg|Grails架构
 +
</gallery>
 +
 
 +
==链接==
 +
*http://grails.org/
 
*http://www.g2one.com/
 
*http://www.g2one.com/
 
*http://www.groovygrails.com
 
*http://www.groovygrails.com
 
*http://download.huihoo.com/grails/
 
*http://download.huihoo.com/grails/
 
*http://docs.huihoo.com/grails/
 
*http://docs.huihoo.com/grails/
[[Image:Grails_logo.jpg|right]]
+
*[http://www.ibm.com/developerworks/cn/java/j-grails/ 精通 Grails 系列]
[[Category:Web application frameworks]]
+
 
 +
[[category:framework]]
 +
[[category:java]]
 +
[[category:groovy]]

2021年7月26日 (一) 10:06的最后版本

Wikipedia-35x35.png 您可以在Wikipedia上了解到此条目的英文信息 Grails Thanks, Wikipedia.
Grails.jpg

Grails关注将"规约编程"(coding by convention)引入Groovy。它是一个开放源网络应用构架, 对Groovy语言起到杠杆作用,并且对Java网络开发起到补足作用。你可以用Grails 作为独立开发环境, 它隐藏了所有的配置细节,也可以将它同和入你的Java商务逻辑。

Grails关注与使开发最简化,而且Grails的解决方案来自于广泛的开发者,不只局限于Java社区。

Grails构建在开源技术如SpringHibernateSiteMesh之上,提供了一个类似于Rails 的平台可以与Java平台无缝集成. Groovy是一种“动态”或指令语言。动态语言一般用来加速和简化程序撰写。与Python或Ruby等其他指令语言不同的是,用Groovy撰写的程序,可与Java虚拟器结合。

目录

[编辑] News

[编辑] 安装

通过sdkman安装Grails

sdk install grails
sdk upgrade grails

通过Homebrew安装Grails

brew install grails
brew upgrade grails

其它方式

下载,如:wget http://download.huihoo.com/grails/grails-bin-1.0.1.zip
GRAILS_HOME= C:\grails-1.0.1 or /usr/local/grails-1.0.1 
Add the "%GRAILS_HOME%\bin" directory to your PATH environment variable
Add the "%GRAILS_HOME%\ant\bin directory to your PATH environment variable.

[编辑] 指南

grails create-app helloworld
cd helloworld
grails create-domain-class book
grails generate-all helloworld.Book
grails -Dserver.port=9000 run-app 或
./grailsw compile
./grailsw -Dreindex=1 run-app
./grailsw -Dserver.port=9000 run-app
http://localhost:9000

grails create-app my-project
cd my-project
grails create-domain-class Book

my-project\grails-app\domain\Book.groovy

class Book {
   String title
   String author
}

my-project\grails-app\conf\BootStrap.groovy

class BootStrap {
   def init = { servletContext ->
       // Create some test data
       new Book(author:"Stephen King",title:"The Shining").save()
       new Book(author:"James Patterson",title:"Along Came a Spider").save()
   }
   def destroy = {
   }
}

my-project\grails-app\controllers\BookController.groovy

class BookController {
    def scaffold = Book
}
grails run-app // or grails dev run-app or grails prod run-app
http://localhost:8080/my-project/
http://localhost:8080/my-project/book
运行成功 :)

To upgrade your existing Grails projects you must run:

grails clean
grails upgrade

[编辑] NetBeans

http://wiki.netbeans.org/groovy

[编辑] PostgreSQL

Grails and PostgreSQL

下载例子: http://docs.huihoo.com/postgresql/regperf.zip

More Detail: http://blogs.sun.com/tomdaly/entry/regperf_v1_running_the_grails

[编辑] CRUD

Create

def p = new Person(name:"Jack", age:32, lastVisit:new Date())
p.save()

Read

def p = Person.get(1)
assert 1 == p.id

Update

def p = Person.get(1)
p.name = "Peter"
p.save()

Delete

def p = Person.get(1)
p.delete()

[编辑] GORM

One-to-one

class Face {
    Nose nose
}
class Nose {	 
	static belongsTo = [face:Face]
}
new Face(nose:new Nose()).save() // ok 
def f = Face.get(1)
f.delete() // both Face and Nose deleted

One-to-many

class Author {
   static hasMany = [ books : Book ]
   String name 
} 
class Book { 
   static belongsTo = [author:Author] 
   String title 
}

Many-to-many

class Book {
  static belongsTo = Author
  static hasMany = [authors:Author]
  String title
}
class Author {
  static hasMany = [books:Book]
  String name
}

Grails 1.0.3 Scaffolding feature does not currently support many-to-many relationship and hence you must write the code to manage the relationship yourself

Grails ManyToMany GORM Example

GPath: Groovy's ability to manipulate collections via GPath and methods like sort, findAll and so on combined with GORM results in a powerful combination.

def books = Book.list()
def books = Book.list(offset:10, max:20)
def books = Book.list(sort:"title", order:"asc")
def book = Book.get(23)
def books = Book.getAll(23, 93, 81)
def book = Book.findByTitle("The Stand")

[编辑] Usage

grails help

  • grails bootstrap
  • grails bug-report
  • grails clean
  • grails compile
  • grails console
  • grails create-app
  • grails create-controller
  • grails create-domain-class
  • grails create-integration-test
  • grails create-plugin
  • grails create-script
  • grails create-service
  • grails create-tag-lib
  • grails create-unit-test
  • grails doc
  • grails generate-all
  • grails generate-controller
  • grails generate-views
  • grails help
  • grails init
  • grails install-plugin
  • grails install-templates
  • grails list-plugins
  • grails package
  • grails package-plugin
  • grails package-plugins
  • grails plugin-info
  • grails release-plugin
  • grails run-app
  • grails run-app-https
  • grails set-proxy
  • grails set-version
  • grails shell
  • grails stats
  • grails test-app
  • grails upgrade
  • grails war

[编辑] Service

Services in Grails are seen as the place to put the majority of the logic in your application, leaving controllers responsible for handling request flow via redirects and so on.

grails create-service simple

[编辑] Plugins

grails list-plugins

To find more info about plugin type 'grails plugin-info [NAME]'

To install type 'grails install-plugin [NAME] [VERSION]'

grails install-plugin yui 2.5.0

For further info visit http://grails.org/Plugins

[编辑] GSP

<html>
   <body>
    <%-- This is my comment --%>  //  <%="Hello GSP!" %>  JSP-style comment
    <%="Hello GSP!" %> 
   </body>
</html>
<%@ page import="java.awt.*" %>
<%@ page contentType="text/json" %>

GSP Tags

<g:example attr="${new Date()}" attr2="[one:'one', two:'two']">
   Hello world
</g:example>

[编辑] Ext

Grails Ext Userinterface
grails install-plugin ext or grails install-plugin ext 2.0.2
Ext has installed under your project's /web-app/js/ext/2.0.2

More Detail: http://grails.org/Ext+Plugin

Using ExtJS Layouts with Grails Layouts

Update on Ext Scaffolding

grails install-templates // when finished the Ext plugin will contain a plugin to install these templates
grails generate-all 

[编辑] GWT

grails install-plugin gwt
grails create-gwt-module <module> 
grails compile-gwt-modules
grails create-gwt-page
grails events
grails run-gwt-client

More Detail: http://www.grails.org/GWT+Plugin

GWTGrailsTutorial: http://code.google.com/p/derjanandhisblog/wiki/GWTGrailsTutorial

[编辑] JSON

def list = {
	def results = Book.list()
	render(contentType:"text/json") {
		books {
			for(b in results) {
				book(title:b.title)
			}
		}	
	}
}

Result

[
	{title:"ActiveRecord Tutorial"}, 
	{title:"Grails Tutorial"}
]

[编辑] Web Flow

Spring Web Flow

Grails supports the creation of web flows built on the Spring Web Flow project.

class BookController {
   …
   def shoppingCartFlow = {
       showCart {
           on("checkout").to "enterPersonalDetails"           
           on("continueShopping").to "displayCatalogue"
       }
       …
       displayCatalogue {
            redirect(controller:"catalogue", action:"show")
       }
       displayInvoice()
   }
}

Web flows don't require an HTTP session, but instead store their state in a serialized form, which is then restored using a flow execution key that Grails passes around as a request parameter. This makes flows far more scalable than other forms of stateful application that use the HttpSession and its inherit memory and clustering concerns.

GrailsFlow is a Workflow and Business Process Engine written in Groovy and Grails.

[编辑] Rules

[编辑] Tutorials

Build Your First Grails Project: A Grails-Powered Blog

[编辑] NetBeans

Groovy and Grails 在开发版中已经可用 http://wiki.netbeans.org/groovy

开发版: http://deadlock.netbeans.org/hudson/job/trunk/lastSuccessfulBuild/artifact/nbbuild/dist/zip/

[编辑] REST

As an example the following mappings provide a RESTful API URL mappings for the BookController:

static mappings = {
   "/product/$id"(controller:"product"){
       action = [GET:"show", PUT:"update", DELETE:"delete", POST:"save"]
   }	
}

[编辑] SOAP

Grails supports SOAP through the XFire plug-in which uses the popular XFire SOAP stack to integrate SOAP support into Grails.

class BookService {
 static expose=['xfire']
 Book[] getBooks()
  { Book.list() as Book[] 
} }
http://127.0.0.1:8080/your_grails_app/services/book?wsdl

[编辑] Ajax

[编辑] Ajax with GWT

[编辑] Ajax with Prototype

[编辑] Ajax with Dojo

[编辑] Validation

class User {
    String login
    String password
    String email
    Integer age
   static constraints = {
       login(size:5..15, blank:false, unique:true)
       password(size:5..15, blank:false)
       email(email:true, blank:false)
       age(min:18, nullable:false)
   }
}

[编辑] Testing

grails test-app SimpleController BookController

[编辑] Unit Testing

[编辑] Integration Testing

[编辑] Functional Testing

Grials support for Canoo WebTest via a plug-in.

Canoo WebTest
grails install-plugin webtest

[编辑] Internationalization

grails-app/i18n

messages.properties
messages_zh.properties
messages_de.properties
messages_es.properties

etc.

[编辑] Security

class SecurityFilters {
  def filters = {
      loginCheck(controller:'*', action:'*') {
          before = {
             if(!session.user && actionName != "login") {
                 redirect(controller:"user",action:"login")
                 return false					
}} } } }

[编辑] Acegi

Grails AcegiSecurity Create User on BootStrap

[编辑] JSecurity

class ExampleController extends JsecAuthBase {
    static accessControl = {
        // All actions require the 'Observer' role.
        role(name: 'Observer')
        // The 'edit' action requires the 'Administrator' 
        role. role(name: 'Administrator', action: 'edit')
        // Alternatively, several actions can be specified. 
        role(name: 'Administrator', only: [ 'create', 'edit', 'save', 'update' ]) }
… }

[编辑] RSS and Atom

Feeds plug-in available for Grails that provides a RSS and Atom builder using the popular ROME library.

def feed = {
    render(feedType:"rss", feedVersion:"2.0") {
        title = "My test feed"
        link = "http://your.test.server/yourController/feed"

        Article.list().each() { 
            entry(it.title) { 
                link = "http://your.test.server/article/${it.id}" 
                it.content // return the content 
} } } }

[编辑] Grails and Spring

[编辑] Grails and Hibernate

[编辑] Scaffolding

class BookController {
   def scaffold = Book
   def changeAuthor = { 
       def b = Book.get( params["id"] ) 
       b.author = Author.get( params["author.id"] ) 
       b.save()
       redirect(action:show) 
} }

[编辑] Template

  • SimpleTemplateEngine – for basic templates
  • GStringTemplateEngine – stores the template as writable closures (useful for streaming scenarios)
  • XmlTemplateEngine – works well when the template and output are valid XM

[编辑] Deployment

grails war /glassfish-v2ur2/domains/domain1/applications/j2ee-modules/blog.war

[编辑] Mobile

Grails on iPhone

[编辑] 相关项目

[编辑] Samples

http://svn.codehaus.org/grails/trunk/grails-samples/

[编辑] 成功应用

http://www.grails.org/Success+Stories

[编辑] 图集

[编辑] 链接

分享您的观点
个人工具
名字空间

变换
操作
导航
工具箱