微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Groovy轻松入门——Grails实战之Web Flow篇

摘要Spring Web Flow是Spring家族中的一位成员,他能简化Web应用的开发。在Grails0.6中就集成了Spring Web Flow这一Web应用开发利器。下面通过一个实例向大家展示一下在Grails中如何方便的使用Spring Web Flow。

Spring Web Flow是Spring家族中的一位成员,他能简化Web应用的开发。在Grails0.6中就集成了Spring Web Flow这一Web应用开发利器。下面通过一个实例向大家展示一下在Grails中如何方便的使用Spring Web Flow。

1,“grails create-app WebFlowDemo”,输出如下:

D:/_DEV/grails_apps>grails create-app WebFlowDemo

Welcome to Grails 0.6 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: D:/D/MY_DEV/grails0.6

Base Directory: D:/_DEV/grails_apps
Environment set to development
Note: No plugin scripts found
Running script D:/D/MY_DEV/grails0.6/scripts/CreateApp.groovy
Overriding prevIoUs deFinition of reference to groovyJarSet
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/src
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFl

owDemo/src/java
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/src/groovy
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/grails-app
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/grails-app/controllers
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/grails-app/services
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/grails-app/domain
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/grails-app/taglib
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/grails-app/utils
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/grails-app/views
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/grails-app/views/layouts
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/grails-app/i18n
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/grails-app/conf
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/test
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/test/unit
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/test/integration
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/scripts
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/web-app
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/web-app/js
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/web-app/css
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/web-app/images
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/web-app/WEB-INF/classes
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/web-app/meta-inf
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/lib
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/grails-app/conf/spring
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/grails-app/conf/hibernate
[propertyfile] Creating new property file:

D:/_DEV/grails_apps/WebFlowDemo/application.properties
[copy] copying 2 files to

D:/_DEV/grails_apps/WebFlowDemo
[copy] copying 2 files to

D:/_DEV/grails_apps/WebFlowDemo/web-app/WEB-INF
[copy] copying 5 files to

D:/_DEV/grails_apps/WebFlowDemo/web-app/WEB-INF/tld
[copy] copying 131 files to

D:/_DEV/grails_apps/WebFlowDemo/web-app
[copy] copying 1 file to

D:/_DEV/grails_apps/WebFlowDemo/grails-app/conf
[copy] copying 13 files to

D:/_DEV/grails_apps/WebFlowDemo/grails-app
[copy] copying 1 file to

D:/_DEV/grails_apps/WebFlowDemo/grails-app/conf/spring
[copy] copying 1 file to

D:/_DEV/grails_apps/WebFlowDemo
[copy] copying 1 file to

D:/_DEV/grails_apps/WebFlowDemo
[copy] copying 1 file to

D:/_DEV/grails_apps/WebFlowDemo
[propertyfile] Updating property file:

D:/_DEV/grails_apps/WebFlowDemo/application.properties
Created Grails Application at D:/_DEV/grails_apps/WebFlowDemo
D:/_DEV/grails_apps>

2,“cd WebFlowDemo”,进入工程目录

3,“grails create-domain-class User”,创建一个域类User,输出如下

D:/_DEV/grails_apps/WebFlowDemo>grails create-domain-class User

Welcome to Grails 0.6 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: D:/D/MY_DEV/grails0.6

Base Directory: D:/_DEV/grails_apps/WebFlowDemo
Environment set to development
Note: No plugin scripts found
Running script

D:/D/MY_DEV/grails0.6/scripts/CreateDomainClass.groovy
[copy] copying 1 file to

D:/_DEV/grails_apps/WebFlowDemo/grails-app/domain
Created  for User
[copy] copying 1 file to

D:/_DEV/grails_apps/WebFlowDemo/test/integration
Created Tests for User
D:/_DEV/grails_apps/WebFlowDemo>


4,将grails-app/domain/User.groovy修改为如下内容

class  User  implements  Serializable {
String username
String password
}

5,“grails generate-all User”,生成所有与域类User相关的目录和文件输出如下

D:/_DEV/grails_apps/WebFlowDemo>grails generate-all User

Welcome to Grails 0.6 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: D:/D/MY_DEV/grails0.6

Base Directory: D:/_DEV/grails_apps/WebFlowDemo
Environment set to development
Note: No plugin scripts found
Running script

D:/D/MY_DEV/grails0.6/scripts/Generateall.groovy
[mkdir] Created dir:

C:/Documents and Settings/Daniel/.grails/0.6/projects/WebFlowDemo/classes
Compiling 5 source files to

C:/Documents and Settings/Daniel/.grails/0.6/projects/WebFlowDemo/classes
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/web-app/WEB-INF/grails-app/i18n
[copy] copying 8 files to

D:/_DEV/grails_apps/WebFlowDemo/web-app/WEB-INF/grails-app/i18n
[copy] copying 1 file to

D:/_DEV/grails_apps/WebFlowDemo/web-app/WEB-INF/spring
[copy] copying 1 file to

C:/Documents and Settings/Daniel/.grails/0.6/projects/WebFlowDemo/classes
[copy] copying 1 file to

C:/Documents and Settings/Daniel/.grails/0.6/projects/WebFlowDemo/classes
Generating views for domain class User
Generating controller for domain class User
Finished generation for domain class User
D:/_DEV/grails_apps/WebFlowDemo>


6,将grails-app/controllers/UserController.groovy修改为如下内容

class  UserController {
def index  =  { redirect(action:list,params:params) }

//  the delete,save and update actions only accept POST requests
def allowedMethods  = 

[delete: ' POST ',save: ' POST ',update: ' POST ' ]

def list  =  {
if ( ! params.max)params.max  =   10
[ userList: User.list( params ) ]
}

def show  =  {
[ user : User.get( params.id ) ]
}

def delete  =  {
def user  =  User.get( params.id )
if (user) {
user.delete()
flash.message  =   " User ${params.id} deleted. "
redirect(action:list)
}
else  {
flash.message  =  

" User not found with id ${params.id} "
redirect(action:list)
}
}

def edit  =  {
def user  =  User.get( params.id )

if ( ! user) {
flash.message  =  

" User not found with id ${params.id} "
redirect(action:list)
}
else  {
return  [ user : user ]
}
}

def update  =  {
def user  =  User.get( params.id )
if (user) {
user.properties  =  params
if (user.save()) {
flash.message  =  

" User ${params.id} updated. "
redirect(action:show,id:user.id)
}
else  {
render(view: ' edit ',model:[user:user])
}
}
else  {
flash.message  =  

" User not found with id ${params.id} "
redirect(action:edit,id:params.id)
}
}

def create  =  {
def user  =   new  User()
user.properties  =  params
return  [ ' user ' :user]
}

def save  =  {
def user  =   new  User()
user.properties  =  params
if (user.save()) {
flash.message  =  

" User ${user.id} created. "
redirect(action:show,id:user.id)
}
else  {
render(view: ' create ',model:[user:user])
}
}

def loginSystemFlow  =  {
login {
on( " login " ) {
if  ( " daniel "  

!=  params.username  ||   " 123 "  

!=  params.password) {
flash.message  =  

" username or password is invalid "

def user = new User()
user.properties = params
flash.user = user

return error ()
} else {
// 仅仅用来演示如何使用flow scope
flow.message =

"Welcome,${params.username}"
}
}.to "ok"
on("register").to "showRegisterView"
}

ok()

showRegisterView()
}
}



on("login"){...}.to "ok"表示点击login按钮后所要去的页面为ok,如果验证失败("daniel" != params.username || "123" != params.password),则利用内置的error(),使用户返回login页面;而on("register").to "showRegisterView"是类似的,不再累述。


7,在grails-app/views/user/目录下新建一个名为“loginSystem”的子目录,该名称为UserController.groovy中的loginSystemFlow的“loginSystem”相对应。

8,在grails-app/views/user/loginSystem/目录下新建login.gsp,ok.gsp,showRegisterView.gsp三个GSP文件(这三个文件文件名与

def loginSystemFlow = {
login {
...
}

ok()

showRegisterView()
}

中粗体突显的state名相对应,其中login为start state,而ok和showRegisterView为end state) ,并将其内容修改为:
login.gsp

< html >
< head >
< Meta  http-equiv ="Content-Type" 

content ="text/html; charset=UTF-8"   />
< Meta  name ="layout"  content ="main"   />
< title > Login Page </ title >
</ head >
< body >
< div  class ="nav" >
< span  class ="menuButton" >< a  class ="home"
href ="${createLinkTo(dir:'')}" > Home </ a >
</ span >
</ div >
< div  class ="body" >
< h1 >
Login Page
</ h1 >
< g:if  test ="${message }" >
< div  class ="message" >
${message }
</ div >
</ g:if >
< g:form  action ="loginSystem"  method ="post" >
< div  class ="dialog" >
< table >
< tbody >
< tr  class ='prop' >
< td  valign ='top'  class ='name' >
< label  for ='username' >
Username:
</ label >
</ td >
< td  valign ='top'  class ='value 

${hasErrors(bean:user,field:'username','errors')}' >
< input  type ="text" 

id ='username'  name ='username'
value ="${user?

.username?.encodeAsHTML()}"   />
</ td >
</ tr >
< tr  class ='prop' >
< td  valign ='top'  class ='name' >
< label  for ='password' >
Password:
</ label >
</ td >
< td  valign ='top'  class =

'value  ${hasErrors(bean:user,field:'password','errors')}' >
< input  type ="text" 

id ='password'  name ='password'
value ="${user?

.password?.encodeAsHTML()}"   />
</ td >
</ tr >
</ tbody >
</ table >
</ div >
< div  class ="buttons" >
< span  class ="button" >< g:submitButton  class ="save"
name ="login"  value ="Login" ></ g:submitButton >
</ span >
</ div >
< g:link  action ="loginSystem"  event ="register" >

Register </ g:link >
</ g:form >
</ div >
</ body >
</ html >

ok.gsp
${message } < br  />

< g:link  action ="loginSystem" > back to login page </ g:link >

showRegisterView.gsp
This is a register page  < br  />
< g:link  action ="loginSystem" > back to login page </ g:link >


9,“grails run-app”,启动Grails应用,输出如下

D:/_DEV/grails_apps/WebFlowDemo>grails run-app

Welcome to Grails 0.6 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: D:/D/MY_DEV/grails0.6

Base Directory: D:/_DEV/grails_apps/WebFlowDemo
Environment set to development
Note: No plugin scripts found
Running script D:/D/MY_DEV/grails0.6/scripts/RunApp.groovy
[mkdir] Created dir:

D:/_DEV/grails_apps/WebFlowDemo/web-app/WEB-INF/lib
Compiling 1 source files to

C:/Documents and Settings/Daniel/.grails/0.6/projects/WebFlowDemo/classes
Running Grails application..
2007-09-08 00:12:24.443::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
2007-09-08 00:12:24.521::INFO:  jetty-6.1.4
2007-09-08 00:12:26.425:/WebFlowDemo:

INFO:  Set web app root system property: '@grails.project.key@'

= [D:/_DEV/grails_apps/WebFlowDemo/web-app/]

2007-09-08 00:12:26.425:/WebFlowDemo:

INFO:  Initializing Log4J from

[D:/_DEV/grails_apps/WebFlowDemo/web-app/WEB-INF/classes/log4j.properties]
2007-09-08 00:12:26.472:/WebFlowDemo:

INFO:  Loading Spring root WebApplicationContext
[9282] hibernate.ConfigurableLocalSessionfactorybean

Building new Hibernate SessionFactory
[9922] mvc.GrailsUrlHandlerMapping Neither 'urlMap'

nor 'mappings' set on SimpleUrlHandlerMapping
[12750] commons.GrailsConfigUtils [GrailsContextLoader] Grails application loaded.
[12797] mvc.GrailsWebRequestFilter Initializing filter 'grailsWebRequest'
[12797] mvc.GrailsWebRequestFilter Filter 'grailsWebRequest' configured successfully
[12797] filter.UrlMappingsFilter Initializing filter 'urlMapping'
[12797] filter.UrlMappingsFilter Initializing filter 'urlMapping'
[12797] filter.UrlMappingsFilter Filter 'urlMapping' configured successfully
[12797] filter.UrlMappingsFilter Filter 'urlMapping' configured successfully
[12797] filter.GrailsReloadServletFilter Initializing filter 'reloadFilter'
[12797] filter.GrailsReloadServletFilter Filter 'reloadFilter' configured successfully
[12891] servlet.GrailsdispatcherServlet Initializing servlet 'grails'
[12954] servlet.GrailsdispatcherServlet

FrameworkServlet 'grails': initialization started
2007-09-08 00:12:36.562:/WebFlowDemo:

INFO:  Loading WebApplicationContext for Spring FrameworkServlet 'grails'
[13485] servlet.GrailsdispatcherServlet

Using context class [org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext]

for servlet 'grails'
[13485] servlet.GrailsdispatcherServlet

Using MultipartResolver [org.springframework.web.multipart

.commons.CommonsMultipartResolver@dd7404]
[13485] servlet.GrailsdispatcherServlet

Using LocaleResolver [org.springframework.web.servlet.i18n.SessionLocaleResolver@ebbf5c]
[13500] servlet.GrailsdispatcherServlet

Unable to locate ThemeResolver with name 'themeResolver':

using default [org.springframework.web.servlet.
theme.FixedThemeResolver@192c766]
[13516] servlet.GrailsdispatcherServlet

No HandlerAdapters found in servlet 'grails': using default
[13532] servlet.GrailsdispatcherServlet

Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator':

using default [org.springfra
mework.web.servlet.view.DefaultRequestToViewNameTranslator@a40575]
[13532] servlet.GrailsdispatcherServlet

Using MultipartResolver

[org.springframework.web.multipart.commons.CommonsMultipartResolver@dd7404]
[13532] servlet.GrailsdispatcherServlet

FrameworkServlet 'grails': initialization completed in 578 ms
[13532] servlet.GrailsdispatcherServlet Servlet 'grails' configured successfully
2007-09-08 00:12:37.374::INFO:  Started SelectChannelConnector@0.0.0.0:8080
Server running. browse to http://localhost:8080/WebFlowDemo


10,访问 http://localhost:8080/WebFlowDemo/user/loginSystem


11,输入任意登录信息,并按一下Login按钮,我直接按Login按钮 :)


12,输入daniel和123,并按一下Login按钮
Welcome,daniel
back to login page

13,点击"back to login page"链接,在点击login页面上的Register链接
This is a register page
back to login page

一个最简单的Spring Web Flow应用完成。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐