Grails:使用index.gsp中的控制器 [英] Grails: use controller from index.gsp

查看:150
本文介绍了Grails:使用index.gsp中的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是grails的新手,我想从我的index.gsp中的特定控制器中使用方法

i am new to grails and i want to use a method from a specific controller in my index.gsp

在Index.gsp中,我试过了

In Index.gsp i tried

<g:each in="${MyController.myList}" var="c">
     <p>${c.name}</p>
</g:each>

但它表示该属性不可用。

but it says that the property is not available.

MyController包含如下属性:

MyController contains a property like:

   def myList = {
       return [My.findAll()  ]
   }

我做错了什么?是否有关于grails-parts之间的沟通的良好教程?

What am i doing wrong? Is there a good tutorial about the communication between the grails-parts?

或者有更好的方法可以通过gsp打印信息吗?

Or is there a better way to get information printed via gsp?

由于

推荐答案

一般地,使用 Model-View-Controller 模式,你不希望你的视图知道关于控制器的任何信息。控制器的工作就是将模型提供给视图。因此,而不是让index.gsp直接响应请求,你应该有一个控制器处理它。然后控制器可以获得必要的所有域对象(模型),并将它们传递给视图。示例:

Generally, when using the Model-View-Controller pattern, you don't want your view to know anything about controllers. It's the controller's job is to give the model to the view. So instead of having index.gsp respond to the request directly, you should have a controller handle it. The controller can then get all the domain objects necessary (the model), and pass them on to the view. Example:

// UrlMappings.groovy
class UrlMappings {
    static mappings = {
        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(controller:"index") // instead of linking the root to (view:"/index")
        "500"(view:'/error')
    }
}

// IndexController.groovy
class IndexController {
    def index() {  // index is the default action for any controller
        [myDomainObjList: My.findAll()] // the model available to the view
    }
}

// index.gsp
<g:each in="${myDomainObjList}" var="c">
    <p>${c.name}</p>
</g:each>

这篇关于Grails:使用index.gsp中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆