0

我有一个用户控制器,它主要是 CRUD。我使用spring工具套件中的生成视图自动生成它

它在视图中创建了一个用户文件夹,并包含列表、_form、创建、编辑、显示 GSP。

package rms

import org.springframework.dao.DataIntegrityViolationException

class UserController {

static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

def index() {
    redirect(action: "list", params: params)
}

def list(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    [userInstanceList: User.list(params), userInstanceTotal: User.count()]
}

def create() {
    [userInstance: new User(params)]
}

def save() {
    def userInstance = new User(params)
    if (!userInstance.save(flush: true)) {
        render(view: "create", model: [userInstance: userInstance])
        return
    }

    flash.message = message(code: 'default.created.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])
    redirect(action: "show", id: userInstance.id)
}

def show(Long id) {
    def userInstance = User.get(id)
    if (!userInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), id])
        redirect(action: "list")
        return
    }

    [userInstance: userInstance]
}

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

    [userInstance: userInstance]
}

def update(Long id, Long version) {
    def userInstance = User.get(id)
    if (!userInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), id])
        redirect(action: "list")
        return
    }

    if (version != null) {
        if (userInstance.version > version) {
            userInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                      [message(code: 'user.label', default: 'User')] as Object[],
                      "Another user has updated this User while you were editing")
            render(view: "edit", model: [userInstance: userInstance])
            return
        }
    }

    userInstance.properties = params

    if (!userInstance.save(flush: true)) {
        render(view: "edit", model: [userInstance: userInstance])
        return
    }

    flash.message = message(code: 'default.updated.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])
    redirect(action: "show", id: userInstance.id)
}

def delete(Long id) {
    def userInstance = User.get(id)
    if (!userInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), id])
        redirect(action: "list")
        return
    }

    try {
        userInstance.delete(flush: true)
        flash.message = message(code: 'default.deleted.message', args: [message(code: 'user.label', default: 'User'), id])
        redirect(action: "list")
    }
    catch (DataIntegrityViolationException e) {
        flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'user.label', default: 'User'), id])
        redirect(action: "show", id: id)
    }
}
}

我在视图中创建了一个管理员 GSP。它有一个页眉、一个页脚和一个侧边栏。内容应位于 class=col-xs-10 的 div 下。我还不能发布图片

<html>
<head>
       <title>Home</title>
    <meta name="layout" content="basic"/>

    <link rel="stylesheet" href="${resource(dir: 'css/RMS', file: 'non-responsive.css')}" type="text/css" media="screen">
    <link rel="stylesheet" href="${resource(dir: 'css/RMS', file: 'style.css')}" type="text/css" media="screen">
    <link rel="stylesheet" href="${resource(dir: 'css/RMS/personnel', file: 'admin.css')}" type="text/css" media="screen">
    <link rel="stylesheet" href="${resource(dir: 'css/RMS/personnel', file: 'tabs.css')}" type="text/css" media="screen">   
</head>
<body>

<g:render template="/layouts/headerAdmin"></g:render>

<div id="wrap">
    <div class="row">
        <div class="col-xs-2">
          <ul class="nav nav-pills nav-stacked" id="sticker">
            <li><a href="admin-reports.html"><strong>Reports</strong></a></li>
            <li><a href="admin-tables.html"><strong>Manage Tables</strong></a></li>
            <li class="active"><a href="admin-employees.html"><strong>Manage Employees</strong></a></li>
            <li><a href="admin-menu.html"><strong>Manage Menu</strong></a></li>
            <li><a href="admin-inventory.html"><strong>Inventory</strong></a></li>
            <li><a href="admin-pos.html"><strong>Point of Sales</strong></a></li>         
          </ul>
        </div>
        <div class="col-xs-10">
        </div>
    </div>
</div>

<script type="text/javascript">
  $('#myTabs a').click(function (e) {
    e.preventDefault()
    $(this).tab('show')
  });
</script>

</body>

您如何在 admin GSP 中使用用户 GSP 作为模板,以便表格出现在 admin 中?基本上列表 gsp 显示了数据库表。这是列表 gsps btw 中的内容

<%@ page import="rms.User" %>
<!DOCTYPE html>
<html>
<head>
    <meta name="layout" content="main">
    <g:set var="entityName" value="${message(code: 'user.label', default: 'User')}" />
    <title><g:message code="default.list.label" args="[entityName]" /></title>
</head>
<body>
    <a href="#list-user" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
    <div class="nav" role="navigation">
        <ul>
            <li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
            <li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li>
        </ul>
    </div>
    <div id="list-user" class="content scaffold-list" role="main">
        <h1><g:message code="default.list.label" args="[entityName]" /></h1>
        <g:if test="${flash.message}">
        <div class="message" role="status">${flash.message}</div>
        </g:if>
        <table>
            <thead>
                <tr>

                    <g:sortableColumn property="username" title="${message(code: 'user.username.label', default: 'Username')}" />

                    <g:sortableColumn property="password" title="${message(code: 'user.password.label', default: 'Password')}" />

                    <g:sortableColumn property="dateOfBirth" title="${message(code: 'user.dateOfBirth.label', default: 'Date Of Birth')}" />

                    <g:sortableColumn property="contactNumber" title="${message(code: 'user.contactNumber.label', default: 'Contact Number')}" />

                    <g:sortableColumn property="employeeID" title="${message(code: 'user.employeeID.label', default: 'Employee ID')}" />

                    <g:sortableColumn property="employeeStatus" title="${message(code: 'user.employeeStatus.label', default: 'Employee Status')}" />

                </tr>
            </thead>
            <tbody>
            <g:each in="${userInstanceList}" status="i" var="userInstance">
                <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">

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

                    <td>${fieldValue(bean: userInstance, field: "password")}</td>

                    <td><g:formatDate date="${userInstance.dateOfBirth}" /></td>

                    <td>${fieldValue(bean: userInstance, field: "contactNumber")}</td>

                    <td>${fieldValue(bean: userInstance, field: "employeeID")}</td>

                    <td>${fieldValue(bean: userInstance, field: "employeeStatus")}</td>

                </tr>
            </g:each>
            </tbody>
        </table>
        <div class="pagination">
            <g:paginate controller="user" total="${userInstanceTotal}" />
        </div>
    </div>
</body>

4

1 回答 1

0

如果您询问如何在该 div 中显示用户控制器列表操作,您可以这样做:

<div class="col-xs-10">
    <g:include controller="user" action="list" />
</div>

你可以在这里阅读更多:http: //grails.org/doc/latest/ref/Tags/include.html

但我认为你真正追求的是创建一个可以在所有管理页面上使用的布局。您可以在此处阅读更多相关信息:http: //grails.org/doc/latest/guide/theWebLayer.html#layouts

该布局会将样式应用于您应用它的任何 GSP,因此您可以创建一个“管理员”布局,然后在您的 CRUD GSP 中您可以:

<meta name="layout" content="admin" />

<head>标签内。

于 2013-09-24T02:38:20.180 回答