How to set a model attribute for each action

I used to use Spring MVC and the @ModelAttribute annotation.

@Controller
public class ArticleController {

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

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

    } 
}

How can this be done in the 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 do not want this.

Many thanks for your help. Tomash

+3
source share
1 answer

You can identify after the interceptor and add data to the model:

class ArticleController {

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

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

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

+4
source

Source: https://habr.com/ru/post/1736619/


All Articles