Grains 控制器中操作的重复代码

发布于 11-15 11:38 字数 848 浏览 3 评论 0原文

我已经发布了这个问题,但我意识到答案不是我想要的。想象一下这个控制器:

    class exampleController{

    def action1 = {
           ...

[lala: lala, lele: lele]}

           ...
    }
    def action15 = {
           ...

[lala: lala, lele: lele]

}

我希望能够在此控制器中的所有操作中返回相同的参数。想象一下:

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

除了在所有操作上编写相同的代码之外,还有什么方法可以做到这一点吗?我已经尝试过这种方法,但它不起作用:

def action5 = {getModel()}

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

它不起作用,因为我的想法是,他不接受多个 [return1: aaa, return2: bbb]。请问有什么建议吗?我还尝试过如下过滤器: Grails 控制器对所有操作重复代码 但我无法让它发挥作用。如果可能的话,我希望获得有关任何解决方案的详细解释:p 提前致谢, VA

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 advanced,
VA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

も星光 2024-11-22 11:38:37

所以它不是同一个模型,而是一个有重复部分的模型。

你应该知道返回值是一个普通的Map

因此,返回值可以像 return getCommonModel() + [book: currentBook] 一样构造,其中 getCommonModel() 返回另一个 Map。

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.

你好,陌生人 2024-11-22 11:38:37

如果您想从所有操作返回相同的模型,则此方法应该可行:

class ExampleController {

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

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

如果您想从所有操作返回相同的模型渲染相同的视图,则可以返回相同的ModelAndView 来自每个操作,但是我会问,如果它们都在做完全相同的事情,为什么需要单独的操作?

我不太明白你的假设

它不起作用,因为我的想法是,他不接受多个[return1: aaa, return2: bbb]

如果您建议 getModel() 只能返回一个模型仅凭一个条目,我发现很难相信。您能否对此进行详细说明,或者发布一些更多信息(例如堆栈跟踪、单元测试)来显示它如何/为什么不起作用?

更新

阅读下面的评论后,我想我终于明白了您想要实现的目标,即将 getModel() (上面)返回的模型附加到各种其他操作返回的模型中。这是否有效:

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
  }
}

只有当您想在单个控制器中添加书籍模型时,此方法才有效。如果您想在多个控制器中添加书籍模型,可以通过以下方式执行此操作:

  • addBookModel 放入控制器扩展的抽象类中
  • addBookModel 放入混合的类中 -与控制器一起使用(使用@Mixin),
  • addBookModel放入在控制器操作之后执行的过滤器中

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
七度光 2024-11-22 11:38:37

如果您在多个页面中使用完全相同的模型。我建议您使用标签库。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文