开发者

How to set model attribute for every action

earlier I use Spring MVC and annotation @ModelAttribute.

@Controller
public class ArticleController {

    @ModelAttribute("articles")
    public List<Testset> getArticles(){
        return articleDao.all();
    }

    @RequestMapping("/first.htm")
    public void first(){                       

    } 
}

How can made this behaviour in Grails controller?

class ArticleController{

    //this variable I want to in every action
    List articles 

    def first = { }   
    def second = { } 
    def third = { } 
}

Of course I can use this code for every action

def first = {
  this.articles = Article.all()
}

but I want not this.

Than开发者_开发问答k very much for your help. Tomáš


You can define an after interceptor and add data to the model there:

class ArticleController {

   def afterInterceptor = { model ->
      model.articles = Article.list()
   }

   def first = { }   
   def second = { } 
   def third = { } 
}

The docs for this are here: http://grails.org/doc/latest/ref/Controllers/afterInterceptor.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜