Groovy on rails 入门 (C -1)

     Grails中的跳转页面大概有以下几种方式:

1. return model

  1. def show = {   
  2.     def b = Book.get( params['id'] )   
  3.     return [ book : b ]   
  4. }  

没有显式返回model时,action会自动把当前controller的property当作model返回,而模版的地址则为<...>/controller/action.gsp

2. return view

  1. import org.springframework.web.servlet.ModelAndView   
  2.   
  3. ...   
  4.   
  5. def index = {   
  6.     def favBooks = // get some books just for the index page, perhaps your favorites   
  7.   
  8.     // forward to the list view to show them   
  9.     return new ModelAndView("/book/list", [ bookList : favBooks ])   
  10. }  

3. redirect

  1. redirect(action:list)  

当跳转到另一个controller的action时,最好使用这种方式,也可以带参数,如:

  1. redirect(controller:'apartment',action:'show',id:apartment.id)  

4. render

  1. // write some markup   
  2. render {   
  3.    for(b in books) {   
  4.       div(id:b.id, b.title)   
  5.    }   
  6. }   
  7. // render a specific view   
  8. render(view:'show')   
  9. // render a template for each item in a collection   
  10. render(template:'book_template', collection:Book.list())   
  11. // render some text with encoding and content type   
  12. render(text:",contentType:"text/xml",encoding:"UTF-8")  

     Grails中变量的scope分类和传统的jsp大致相同,分为servletContext, session, request, flash. 比较新颖的是flash,意思是在当前request和下一个request中保持,这样一来处理消息和错误就非常方便了。

  1. def findBy = params["findBy"]
  2. def appContext = servletContext["appContext"]
  3. def loggedUser = session["logged_user"]
  4. flash['message'] = "User not found for id ${params['id']}"  

    因此,request的信息,grails不会像OGNL那样设置到controller中去,而要从params里面取,比如params.id 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值