如何使表格中的项目列表中的链接工作

时间:2012-03-13 08:08:55

标签: grails show details

我想从此页面创建一个链接,导航到详细信息页面 但是我收到了404错误。

    Here is the gsp:
<%@ page import="tictoc.Store" %>
    <!doctype html>
    <html>
        <head>
            <meta name="layout" content="main">
            <g:set var="entityName" value="${message(code: 'store.label', default: 'Store')}" />
            <title><g:message code="default.show.label" args="[entityName]" /></title>
        </head>
        <body>

            <h1> Ticket Store </h1>
            <div>
                <table border=0 class="eventsTable">
                    <tr>

                        <th>Event</th>
                        <th>Start </th>
                        <th>End </th>
                        <th>Description</th>
                    </tr>

                    <g:each var="event" in="${events}">

                    <tr>
                        <td><g:link action="display" controller="store" id="${event.id}">
                            ${event.name}
                            </g:link>
                        </td>
                        <td><g:formatDate format="MM/dd/yyyy" date="${event.startDate}"/></td>
                        <td><g:formatDate format="MM/dd/yyyy" date="${event.endDate}"/></td>
                        <td>${event.desc}</td>
                    </tr>

                    </g:each>

                </table>

            </div>
            <div class="paginateButtons">
                <g:paginate controller="store"
                    action="show"
                    max="5"
                    params="[name:active]"
                    total="${totalEvents}" />
            </div>
        </body>
    </html>

这是控制器,我有一个名为buy.gsp的GSP,永远不会显示出来。

        package tictoc

    import tictoc.Event 

    class StoreController {

        Event event
        static defaultAction = "activeEvents"

        def show = {
            log.error 'exec activeEvents'
            activeEvents()
        }

        def activeEvents = {
            log.error 'exec activeEvents'
            params.max = Math.min(params.int('max', 5), 100);
            params.offset = params.int('offset', 0)
            params.total  =  Event.count()
            params.sort   = params.sort ?: "name";
            params.order  = params.order ?: "asc";

            //  def max    = Math.min(params.int('max', 5), 100);
            //  def offset = params.offset?.toInteger() ?: 0
            //  def total =  Event.count()
            def eventList = Event.list(max: params.max, offset: params.offset)

            return [events:eventList, 
                    totalEvents:params.total,
                    active:params.active]                  
        }

        def detail = {
            log.error 'exec detail'
            def eventId = params.id
            log.error 'eventId:' + eventId
            render(view: "buy", model: [event: eventId])
        }

        def list = {
            params.max = Math.min(params.int('max', 25), 100);
            params.offset = params.int('offset', 0)
            params.sort = params.sort ?: "name";
            params.order = params.order ?: "asc";
            return [ myList: DomainObject.list(params) ]
        }


        def display = {
            println "display..........."
            def event = Event.findById(params.id)
            if(event) {
                println "EVENT IS $event"
                render(view:"buy", model:[event:event])
            }
            else {
                response.sendError 404
            }
        }
    }

Here is the URL Mapping:

class UrlMappings { static mappings = { "/"(controller:"store", action:'show') } }

3 个答案:

答案 0 :(得分:0)

您可以尝试将Event.findById(params.id)更改为Event.get(params.id)

您的活动似乎为空。

答案 1 :(得分:0)

您没有任何网址/store/display/6的映射,因为您只将一个指定的控制器和指定的操作映射到精确网址,方法是:

mappings = { "/"(controller:"store", action:'show') } 

如果您需要映射diplay,则必须返回(可能已删除)映射:

class UrlMappings {

    static mappings = {
        "/$controller/$action?/$id?"{
             constraints {
                // apply constraints here
             }
        }
        "/"(controller:"store", action:'show') 
    }
}

您可以在http://grails.org/doc/latest/guide/theWebLayer.html#urlmappings

了解有关网址映射的更多信息

答案 2 :(得分:0)

您还可以提供更具体的映射:

class UrlMappings {

    static mappings = {
        "/displayEvent/$id"(controller: 'store', action: 'display')
        "/"(controller:"store", action:'show') 
    }
}