我们需要准备三个东西:页面,控制器,实体
首先我们需要在view创建一个页面:名为index.gsp
<%@ page import="com.zfq.Book" %>
<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main">
<g:set var="entityName" value="${message(code: 'book.label', default: 'Book')}"/>
<title><g:message code="default.list.label" args="[entityName]"/></title>
</head>
<body>
<a href="#list-book" class="skip" tabindex="-1"><g:message code="default.link.skip.label"
default="Skip to content…"/></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-book" 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>
<g:form url="[resource: bookInstance, action: 'queryInfoByCondition']" method="GET">
<label for="title">
<g:message code="book.title.label" default="Title"/>
<span>:</span>
</label>
<g:textField name="title"/>
<label for="author">
<g:message code="book.author.label" default="Author"/>
<span>:</span>
</label>
<g:textField name="author"/>
<g:actionSubmit class="save" action="queryInfoByCondition"
value="Search"/>
</g:form>
<table>
<thead>
<tr>
<g:sortableColumn property="id" title="${message(code: 'book.title.label', default: 'id')}"/>
<g:sortableColumn property="title" title="${message(code: 'book.title.label', default: 'Title')}"/>
<g:sortableColumn property="author" title="${message(code: 'book.author.label', default: 'Author')}"/>
</tr>
</thead>
<tbody>
<g:each in="${bookInstanceList}" status="i" var="bookInstance">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td><g:link action="show"
id="${bookInstance.id}">${fieldValue(bean: bookInstance, field: "id")}</g:link></td>
<td><g:link action="show"
id="${bookInstance.id}">${fieldValue(bean: bookInstance, field: "title")}</g:link></td>
<td>${fieldValue(bean: bookInstance, field: "author")}</td>
</tr>
</g:each>
</tbody>
</table>
<div class="pagination">
<g:paginate total="${bookInstanceCount ?: 0}"/>
</div>
</div>
</body>
</html>
实体
package com.zfq
class Book {
String id;
String title
String author
static constraints = {
title(blank: false)
author(blank: false)
}
static mapping={
id generator: "uuid.hex", type:"string"
}
}
控制器
class BookController {
// 显示信息根据查询条件
def queryInfoByCondition={
if (!params.max) params.max = 10
if (!params.offset) params.offset = 0
if (!params.sort) params.sort = "id"
if (!params.order) params.order = "asc"
def cel = {
if(params.title){
like("title", "%${params.title}%")
}
if(params.author){
like("author", "%${params.author}%")
}
}
def results = Book.createCriteria().list(params,cel)
// def cut = Book.createCriteria().count(cel)
render(view: "index",model: [bookInstanceList: results, bookInstanceCount: results.totalCount])
}
}
cel是数据库的查询条件