以GDepot为例:
1.创建搜索页面(searchForm.gsp):
代码如下:
<%@ page import="gdepot.Goods" %>
<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main">
<g:set var="entityName" value="${message(code: 'goods.label', default: 'Goods')}" />
<title>Search goods</title>
</head>
<body>
<a href="#edit-goods" 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="list" action="index"><g:message code="default.list.label" args="[entityName]" /></g:link></li>
</ul>
</div>
<div id="edit-goods" class="content scaffold-edit" role="main">
<g:form method="get" action="search">
<div class="fieldcontain required">
<label for="category">
Category:</label>
<g:select optionKey="categoryName" optionValue="categoryName" from="${gdepot.Category.list()}"name="categoryName" noSelection="${['':'']}"/></br>
</div>
<div class="fieldcontain required">
<label for="category">
Title:</label>
<input type="text" id="title" name="title"/>
</div>
<div class="fieldcontain required">
<label for="category">
Description:</label>
<textarea id="description" name="description" ></textarea>
</div>
<div class="fieldcontain required">
<label for="category"> Price:</label> <input type="text" id="priceLow" name="priceLow"/>
to: <input type="text" id="priceHigh" name="priceHigh"/>
</div>
<fieldset class="buttons">
<g:actionSubmit class="save" action="search" value="Search" />
</fieldset>
</g:form>
</div>
</body>
</html>
第二步:找到 GoodsController.groovy文件
添加一个空方法:def searchForm(){};或者空的闭包:def searchForm={}
第三步:添加一个闭包或者方法(def search={})方法名要和搜索页面form的action一致(如上红色加下划线处)
代码如下:
def search={
if(!params.max)params.max=4 //这里的params.max=4是指一业所要显示的最多个数,此处为每页显示4条信息
def searchClosure={
if(params.categoryName){
category{
eq('categoryName',params.categoryName)
}
}
if(params.title){
like('title',"%${params.title}%")
}
if(params.priceLow){
ge('price',new BigDecimal(params.priceLow))
}
if(params.priceHigh){
le('price',new BigDecimal(params.priceHigh))
}
if(params.description){
like('description',"%${params.description}%")
}
}
def c=Goods.createCriteria()
def goodsList=c.list(params,searchClosure)//此处有两个参数,意思是第一个:一次查询的个数,第二个是提交上来的查询条件
def goodsCount=Goods.count //求满足条件的记录数
render(view:'index',model:[goodsInstanceList:goodsList,goodsInstanceCount:goodsCount])//发送到view页面,且同时发送两个参数过去
}
view(index.gsp)页面代码如下:
<%@ page import="gdepot.Goods" %>
<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main">
<g:set var="entityName" value="${message(code: 'goods.label', default: 'Goods')}" />
<title><g:message code="default.list.label" args="[entityName]" /></title>
</head>
<body>
<a href="#list-goods" 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>
<li><g:link class="search" action="searchForm">Search Goods</g:link></li>
</ul>
</div>
<div id="list-goods" 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>
<th><g:message code="goods.title.label" default="Title" /></th>
<g:sortableColumn property="desciption" title="${message(code:'goods.description.label',default:'Description')}"/>
<g:sortableColumn property="photoUrl" title="${message(code: 'goods.photoUrl.label', default: 'Photo Url')}" />
<g:sortableColumn property="price" title="${message(code: 'goods.price.label', default: 'Price')}" />
<g:sortableColumn property="category" title="${message(code: 'goods.category.label', default: 'Category')}" />
</tr>
</thead>
<tbody>
<g:each in="${goodsInstanceList}" status="i" var="goodsInstance">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td><g:link action="show" id="${goodsInstance.id}">${fieldValue(bean: goodsInstance, field: "title")}</g:link></td>
<td>${fieldValue(bean: goodsInstance, field: "description")}</td>
<td><img src='${resource(dir:"images/myimage",file:"${goodsInstance.photoUrl}")}'/></td>
<td>${fieldValue(bean: goodsInstance, field: "price")}</td>
<td>${fieldValue(bean: goodsInstance, field: "category")}</td>
</tr>
</g:each>
</tbody>
</table>
<div class="pagination">
<g:paginate total="${goodsInstanceCount ?: 0}" />//分页设置
</div>
</div>
</body>
</html>