Grails:使用CRUD GSP作为模板

时间:2013-09-24 02:17:03

标签: grails

我有一个UserController,主要是CRUD。我使用spring tooluite中的generate视图自动生成它

它在视图中创建了一个用户文件夹,包含列表,_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>

如何在管理GSP中将用户GSP用作模板,以便表格显示在管理员中?基本上,列表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>

1 个答案:

答案 0 :(得分: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>标记内。