开发者

Grains repetead code for actions in controllers

I have already posted this question, but i realised the aswer was not what i was looking for. Imagine this controller:

    class exampleController{

    def action1 = {
           ...

[lala: lala, lele: lele]}

           ...
    }
    def action15 = {
           ...

[lala: lala, lele: lele]

}

I want to be able to return in all the action in this controller the same params. Imagining this:

def book = Book.findAllByIsbn(Isbn.get(1))  
[book: book]

Is there any way of doing this, besides writing all the same code on all the actions? I have tried this method and it isnt working:

def action5 = {getModel()}

  private getModel() {
    def book = Book.findAllByIsbn(Isbn.get(1))  
[book: book]  
  }
}

It is not working because, and my thought is, he doest accept multiple [return1: aaa, return2: bbb]. Any suggestion please ? I have also tried filters like in here: Grails controllers repeated code for all actions but i couldnt managed to make it work. I would apreciated a detailed explanaintion about any of the solutions if possible:p Thanks in adva开发者_如何学Gonced, VA


So it's not the same model, but a model with a repeated part.

You should know that the return value is an ordinary Map.

So, return value can be constructed like return getCommonModel() + [book: currentBook] where getCommonModel() returns another Map.


If you want to return the same model from all your actions, this approach should work:

class ExampleController {

  def action5 = {getModel()}
  def action1 = {getModel()}
  //etc.

  private getModel() {
    def book = Book.findAllByIsbn(Isbn.get(1))  
    [book: book]  
  }
}

If you want to return the same model and render the same view from all your actions, you could return the same ModelAndView from each action, but then I would ask why do you need separate actions if they're all doing exactly the same thing?

I don't really understand your hypothesis

It is not working because, and my thought is, he doest accept multiple [return1: aaa, return2: bbb]

If your suggesting that getModel() can only return a model with a single entry, I find that very hard to believe. Can you elaborate a bit on this, or post some more information (e.g. stacktrace, unit test) that shows how/why it's not working?

Update

After reading your comments below I think I finally understand what you want to achieve, which is to append the model returned by getModel() (above) to the model returned by various other actions. Does this work:

class ExampleController {

  def action5 = {
    def action5Model = [foo: 'bar']
    return addBookModel(action5Model)
  }

  def action1 = {
    def action1Model = [foo2: 'bar2']
    return addBookModel(action1Model)
  }
  //etc.

  private Map addBookModel(Map model) {
    def book = Book.findAllByIsbn(Isbn.get(1))  
    model.book = book
    return model
  }
}

This approach will only work when you want to add the book model within a single controller. If you want to add the book model in several controllers you can do this by:

  • putting addBookModel in an abstract class that the controllers extend
  • putting addBookModel in a class that is mixed-in with the controllers (using @Mixin)
  • putting addBookModel in a filter that is executed after the controller actions


If you are using exact same model in multiple pages. I would recommend you use a taglib for it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜