我是grails的新手,我想使用index.gsp中特定 Controller 的方法
我在Index.gsp中尝试过
<g:each in="${MyController.myList}" var="c">
<p>${c.name}</p>
</g:each>
但它表示该属性不可用。
MyController包含如下属性:
def myList = {
return [My.findAll() ]
}
我究竟做错了什么?关于grails-parts之间的通信是否有很好的教程?
还是有更好的方法来通过gsp打印信息?
谢谢
最佳答案
通常,在使用Model-View-Controller模式时,您不希望 View 对 Controller 有所了解。 Controller 的工作是将模型提供给 View 。因此,应该让 Controller 处理它,而不是让index.gsp直接响应请求。然后, Controller 可以获取所有必要的领域对象(模型),并将其传递给 View 。例子:
// 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中的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4279134/