Grails:使用index.gsp中的 Controller

标签 grails gsp

我是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/

相关文章:

hibernate - Grails将Java 8域对象的LocalDateTime映射到Gorm

javascript - 从 GSP 到 Controller 的 Grails 参数为空

grails - 使用远程链接gsp时如何将一个字段的值设置为参数

grails - jsp 表单标签是否有 Grails 等价物?

database - 无法手动将数据保存在Grails中的数据库中,但是使用dbconsole可以很好地工作

grails - 如何重定向到关于拒绝 Spring 安全访问的上一页?

grails - 将复选框值从Grails中的GSP传递给 Controller

image - grails在文件夹中显示图像

grails - 使用连接表+额外的列使很多人陷入困境

hibernate - 在 GORM 中查询计算字段