[Grails] 最近使用grails1.3构建自己的查询条件遇到了问题
kkg123
2010-07-08
我使用grails1.3http://www.oschina.net/action/project/go?id=916&p=download按照《Getting Started with Grails》http://dl.iteye.com/topics/download/db26ca8e-f9bd-46a8-8305-20d753e396de第五章中所讲的方法,在src目录下新建一个raceQuery.java 保存查询条件。但是我在raceController中声明raceQuery时,不识别?不知道哪里出问题了!raceController部分代码如下:
def search={ if(request.method=='POST'){ RaceQuery query=new RaceQuery() bindData(query,params) def criteria=Race.createCriteria() def results=criteria{ and{ like('city','%'+query.city+'%') like('state','%'+query.state+'%') if(query.distance){ switch(query.distanceOperator){ case RaceQuery.DistanceOperator.AT_LEAST: ge('distance',query.distance) break case RaceQuery.DistanceOperator.EXACTLY: eq('distance',query.distance) break case RaceQuery.DistanceOperator.AT_MOST: le('distance',query.distance) break default: log.error "Found unexpected value for distance"+"operator -${query.distanceOperator}" } } between('starDateTime',query.minDate,query.maxDate+1) }//and }//def results render(view:'searchresults',model:[raceInstanceList:results]) }//if } java代码如下: import java.util.Date; public class RaceQuery { public enum DistanceOperator{AT_LEAST,EXACTLY,AT_MOST}; public Date minDate; public Date maxDate; public String city; public String state; public Float distance; public DistanceOperator distanceOperator; public String getCity(){ return city; } public void setCity(String city){ this.city=city; } public Date getMinDate() { return minDate; } public void setMinDate(Date minDate) { this.minDate = minDate; } public String getState() { return state; } public void setState(String state) { this.state = state; } public Date getMaxDate() { return maxDate; } public void setMaxDate(Date maxDate) { this.maxDate = maxDate; } public Float getDistance() { return distance; } public void setDistance(Float distance) { this.distance = distance; } public DistanceOperator getDistanceOperator() { return distanceOperator; } public void setDistanceOperator(DistanceOperator distanceOperator) { this.distanceOperator = distanceOperator; } } 查询页面代码如下: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="layout" content="main" /> <g:set var="entityName" value="${message(code: 'race.label', default: 'Race')}"/> <title><g:message code="default.search.label" args="[entityName]" /></title> <!-- <title>Search for Races</title> --> </head> <body> <div class="nav"> <span class="menuButton"><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></span> <span class="menuButton"><g:link class="list" action="list"><g:message code="default.list.label" args="[entityName]" /></g:link></span> </div> <div class="body"> <h1>Search for Races</h1> <g:if test="${flash.message}"> <div class="message">${flash.message}</div> </g:if> <g:hasErrors bean="${raceInstance}"> <div class="errors"> <g:renderErrors bean="${raceInstance}" as="list" /> </div> </g:hasErrors> <g:form action="search" method="post" > <div class="dialog"> <table> <tbody> <tr class="prop"> <td valign="top" class="name"> <label for="city"><g:message code="race.city.label" default="City" /></label> </td> <td valign="top" class="value ${hasErrors(bean: raceInstance, field: 'city', 'errors')}"> <g:textField name="city" maxlength="30" value="${raceInstance?.city}" /> </td> </tr> <tr class="prop"> <td valign="top" class="name"> <label for="state"><g:message code="race.state.label" default="State" /></label> </td> <td valign="top" class="value ${hasErrors(bean: raceInstance, field: 'state', 'errors')}"> <g:select name="state" from="${new Race().constraints.state.inList}" valueMessagePrefix="race.state" /> </td> </tr> <tr class="prop"> <td valign='top' class='name'> <label for="date">Date:</label> </td> <td valign='top' class='value'> <label> between<g:datePicker name="minDate" precision="day"/> and<g:datePicker name="maxDate" precision="day" /> </label> </td> </tr> <tr class="prop"> <td valign='top' class='name'> <label for="distance">Distance:</label> </td> <td valign="top" class='value'> <select name='distanceOperator'> <option value='AT_LEAST'>At least</option> <option value='EXACTLY'>Exactly</option> <option value='AT_MOST'>At most</option> </select> <!-- <input type="text" name="distance" size='5'>km</input> --> <g:textField name="distance" value="${fieldValue(bean: raceInstance, field: 'distance')}" /> km </td> </tr> </tbody> </table> </div> <div class="buttons"> <!-- <span class="button"><input type="submit" value="Search" ></span> --> <span class="button"><g:submitButton name="search" class="save" value="${message(code: 'default.button.search.label', default: 'Search')}" /></span> </div> </g:form> </div> </body> </html> |