Grails:使用index.gsp中的controller

elC*_*ano 8 grails gsp

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

在Index.gsp我试过

<g:each in="${MyController.myList}" var="c">
     <p>${c.name}</p>
</g:each>
Run Code Online (Sandbox Code Playgroud)

但它说该物业不可用.

MyController包含一个属性,如:

   def myList = {
       return [My.findAll()  ]
   }
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?关于grails-parts之间的通信是否有一个很好的教程?

或者有没有更好的方法来通过gsp打印信息?

谢谢

ata*_*lor 18

通常,在使用模型 - 视图 - 控制器模式时,您不希望视图知道有关控制器的任何信息.控制器的工作是将模型提供给视图.因此,不应让index.gsp直接响应请求,而应该让控制器处理它.然后,控制器可以获取所有必需的域对象(模型),并将它们传递给视图.例:

// 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>
Run Code Online (Sandbox Code Playgroud)