在我的 grails 应用程序中,我需要从数据库中获取一些数据并将其显示在 gsp 页面中。
我知道我需要从 Controller 获取数据,例如

List<Event> todayEvents = Event.findAllByStartTime(today)

获取今天的所有事件
现在,如何在 gsp 页面中呈现它?如何将事件对象列表传递给 gsp?

非常感谢

最佳答案

您可以使用 Grails 脚手架学习许多基本概念。使用域创建一个新项目并发出命令 generate-all com.sample.MyDomain 它将为您生成一个 Controller 和一个 View 。

要回答您的问题,请在 Controller 中创建一个 Action ,如下所示:

class EventController {

    //Helpful when controller actions are exposed as REST service.
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def showEvents() {
        List<Event> todayEvents = Event.findAllByStartTime(today)
        [eventsList:todayEvents]
    }
}

在您的 GSP 上,您可以遍历列表并根据需要打印它们
<g:each in="${eventsList}" var="p">
 <li>${p}</li>
</g:each>

祝你好运

关于database - grails:在 gsp 中显示数据库中的元素列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17426953/

10-11 21:56
查看更多