Grails在Index中创建列表

时间:2015-04-13 08:31:27

标签: grails gorm gsp

在我的索引页面上,我想显示另一个类的所有实例。考虑到它们以相同的方式在gsp中显示所有控制器,应该不会很难。

<!DOCTYPE html>
    <html>
    <head>
        <meta name="layout" content="main"/>
        <title>College Now Portal</title>
        <style type="text/css" media="screen">
            #status {
            background-color: #eee;
            border: .2em solid #fff;
            margin: 2em 2em 1em;
            padding: 1em;
            width: 12em;
            float: left;
            -moz-box-shadow: 0px 0px 1.25em #ccc;
            -webkit-box-shadow: 0px 0px 1.25em #ccc;
            box-shadow: 0px 0px 1.25em #ccc;
            -moz-border-radius: 0.6em;
            -webkit-border-radius: 0.6em;
            border-radius: 0.6em;
        }

        .ie6 #status {
            display: inline; /* float double margin fix http://www.positioniseverything.net/explorer/doubled-margin.html */
        }

        #status ul {
            font-size: 0.9em;
            list-style-type: none;
            margin-bottom: 0.6em;
            padding: 0;
        }

        #status li {
            line-height: 1.3;
        }

        #status h1 {
            text-transform: uppercase;
            font-size: 1.1em;
            margin: 0 0 0.3em;
        }

        #page-body {
            margin: 2em 1em 1.25em 18em;
        }

        h2 {
            margin-top: 1em;
            margin-bottom: 0.3em;
            font-size: 1em;
        }

        p {
            line-height: 1.5;
            margin: 0.25em 0;
        }

        #controller-list ul {
            list-style-position: inside;
        }

        #controller-list li {
            line-height: 1.3;
            list-style-position: inside;
            margin: 0.25em 0;
        }

        @media screen and (max-width: 480px) {
            #status {
                display: none;
            }

            #page-body {
                margin: 0 1em 1em;
            }

            #page-body h1 {
                margin-top: 0;
            }
        }
    </style>
</head>
<body>
<sec:ifAllGranted roles="ROLE_ADMIN">
    <a href="#page-body" class="skip"><g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
    <div id="status" role="complementary">
        <h1>Application Status</h1>
        <ul>
            <li>App version: <g:meta name="app.version"/></li>
            <li>Grails version: <g:meta name="app.grails.version"/></li>
            <li>Groovy version: ${GroovySystem.getVersion()}</li>
            <li>JVM version: ${System.getProperty('java.version')}</li>
            <li>Reloading active: ${grails.util.Environment.reloadingAgentEnabled}</li>
            <li>Controllers: ${grailsApplication.controllerClasses.size()}</li>
            <li>Domains: ${grailsApplication.domainClasses.size()}</li>
            <li>Services: ${grailsApplication.serviceClasses.size()}</li>
            <li>Tag Libraries: ${grailsApplication.tagLibClasses.size()}</li>
        </ul>
        <h1>Installed Plugins</h1>
        <ul>
            <g:each var="plugin" in="${applicationContext.getBean('pluginManager').allPlugins}">
                <li>${plugin.name} - ${plugin.version}</li>
            </g:each>
        </ul>
    </div>
</sec:ifAllGranted>
    <div id="page-body" role="main">
        <sec:ifLoggedIn>
                <p>Logged in as ${sec.username()} | <g:link controller='Logout' action=''>logout</g:link></p>
            </sec:ifLoggedIn>
            <sec:ifNotLoggedIn>
                <p>Welcome to College Now Portal, please <g:link controller='Login' action=''>login</g:link></p>
            </sec:ifNotLoggedIn>

        <div id="controller-list" role="navigation">

            <ul>
                <%--
                COMMENTED OUT GSP CONTROLLER LIST
                --%>
                <g:each var="c" in="${grailsApplication.controllerClasses.sort { it.shortName } }">
                    <li class="controller"><g:link controller="${c.logicalPropertyName}">${c.shortName}</g:link></li>
                </g:each>

                <sec:ifAnyGranted roles="ROLE_ADMIN,ROLE_COORDINATOR,ROLE_LIAISON,ROLE_TEACHER">
                <h1>Select a group to manage:</h1>
                    <h1 style="background-color:black; color:white; width:50%">People</h1>

                    <li class="controller"><a href="/collegenowapp/liaison/index">Liaisons</a></li>
                </sec:ifAnyGranted>
                <sec:ifAnyGranted roles="ROLE_ADMIN,ROLE_COORDINATOR">
                    <li class="controller"><a href="/collegenowapp/student/index">Students</a></li>
                </sec:ifAnyGranted>
                <sec:ifAnyGranted roles="ROLE_ADMIN,ROLE_COORDINATOR,ROLE_LIAISON">
                    <li class="controller"><a href="/collegenowapp/teacher/index">Teachers</a></li>
                </sec:ifAnyGranted>

                <sec:ifAnyGranted roles="ROLE_ADMIN,ROLE_COORDINATOR,ROLE_LIAISON,ROLE_TEACHER">
                <h1 style="background-color:black; color:white; width:50%">Items</h1>
                    <li class="controller"><a href="/collegenowapp/book/index">Books</a></li>
                <sec:ifAnyGranted roles="ROLE_ADMIN,ROLE_COORDINATOR,ROLE_LIAISON"> 
                    <li class="controller"><a href="/collegenowapp/course/index">Courses</a></li>
                    <li class="controller"><a href="/collegenowapp/discipline/index">Disciplines</a></li>
                    <li class="controller"><a href="/collegenowapp/school/index">Schools</a></li>
                    <li class="controller"><a href="/collegenowapp/section/index">Sections</a></li>
                </sec:ifAnyGranted>
                </sec:ifAnyGranted>

            </ul>
        </div>
    </div>
</div>
    <!-- SHOWING MEETINGS ON INDEX PAGE-->
            <table>
        <thead>
                <tr>

                    <th><g:message code="meetings.liaison.label" default="Liaison" /></th>

                    <th><g:message code="meetings.place.label" default="Place" /></th>

                    <th><g:message code="meetings.teacher.label" default="Teacher" /></th>

                    <th><g:message code="meetings.time.label" default="Time" /></th>

                </tr>
            </thead>
            <tbody>
            <g:each in="${meetingsInstanceList}" var="meetingsInstance">
                <tr>

                    <td><g:link action="show" id="${meetingsInstance.id}">${fieldValue(bean: meetingsInstance, field: "liaison")}</g:link></td>

                    <td>${fieldValue(bean: meetingsInstance, field: "place")}</td>

                    <td>${fieldValue(bean: meetingsInstance, field: "teacher")}</td>

                    <td><g:formatDate format="MM-dd-yyyy' at 'hh:mm' 'a" date="${meetingsInstance.time}" /></td>

                    </tr>
                </g:each>
                </tbody>
            </table>
            <div class="pagination">
                <g:paginate total="${meetingsInstanceCount ?: 0}" />
               </div>

    </body>
</html>

我无法弄清楚如何为索引页面编写查询。文档非常小。任何有关查询的帮助请。这段代码几乎可行。我得到的清单返回空白。如果我在上面扔了一个标签,它就说列表为空。它不会读到什么。

1 个答案:

答案 0 :(得分:0)

您可以通过转到Grails App中的 conf 文件夹轻松完成此操作。打开UrlMappings.groovy文件。

现在您将看到以下行

"/"(view:"/index")

您必须将其更改为指向某个控制器/操作

"/"(controller:"CustomController", action:"index")

在CustomController中,您可以重用视图&#34; / index&#34;。 Forexample,

class CustomController{

    def customService

    def index() {
        def retrievedData = customService.retrieveData(paramsIfNecessary)

        render(view:"/index", model: retrievedData)
    }
    .....
}