当链接有id(或没有id)时,如何使布局在grails中工作

how to make layout work in grails when link has id (or not)

本文关键字:id 布局 何使 工作 grails 链接      更新时间:2023-09-26

以下是我在land.gsp页面中的代码。

 <table style="padding: 10 ">
                <thead>
                    <tr style="color: blue">
                        <td>Gname</td>
                        <td>Gowner</td>
                        <td>Device number</td>
                        <td>Edit </td>
                        <td>Delete </td>

                    </tr>
                </thead>
                <tbody>
                <g:each in="${Groups.list()}" status="i" var="groupsInstance">
                <g:set var="myid" value="${groupsInstance.id}"></g:set>
                    <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
                        <td>${fieldValue(bean: groupsInstance, field: "gname")}</td>
                        <td>${fieldValue(bean: groupsInstance, field: "gowner")}</td>
                        <td>${fieldValue(bean: groupsInstance, field: "devicenum")}</td>
                        <td><g:link action="edit" id="${groupsInstance.id}">Edit</g:link></td>
                        <td><g:link action="deleteme" id="${groupsInstance.id}">Delete</g:link></td>
                    </tr>
                </g:each>
                </tbody>
            </table> 

但当我点击编辑时,页面的布局不起作用(javascript、图像、样式)。但是id被正确地传递到edit.gsp页面。当我更改时

<td><g:link action="edit" id="${groupsInstance.id}">Edit</g:link></td>

<td><g:link action="edit" >Edit</g:link></td>

也就是说,不需要传递id,点击edit就会得到一个布局正确的页面。以下是我的编辑操作

def edit(Long id) {
        def groupsInstance = Groups.get(id)
        if (!groupsInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'groups.label', default: 'Groups'), id])
            redirect(action: "list")
            return
        }
        [groupsInstance: groupsInstance]
    }

我哪里错了?我想把id传给所有正确布局的编辑页面。

尝试在浏览器中查看源html,这可能会为您提供错误的线索。

根据您的喜好,尝试采用以下选项:

<g:link action="/conference/participated" id="${it.conference.id}" 
               params="[foo: 'bar', bar: 'foo']">My Link!</g:link>

我在你的代码中看到,当

def groupsInstance = Groups.get(id)
        if (!groupsInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'groups.label', default: 'Groups'), id])
            redirect(action: "list")
            return

您已将其重定向到列表页,请确保列表页有不同的布局设置,这就是更改,请检查并告诉我!

从你的编辑操作来看,如果没有提供id,那么你重定向到列表操作,你会说那里的布局很好。如果指定了id(并且它是有效的),那么您将呈现编辑视图。我想问题出在你的edit.gsp中,而你的描述中目前没有显示。