Grails小技巧
一、Controlller中params
Controlller中params是grails框架中的GrailsParameterMap類,繼承自TypeConvertingMap,而不是一個簡單的Map,
除了支持普通的Map方法以外,還有其他幾個方法非常有用
若需要得到數值類型的參數就非常方便了
二、分頁
其實使用Grails做分頁功能是最easy的事情,因為Domain類的Criteria的list方法返回的結果就是帶有分頁所用信息的PagedResultList類
Domain的動態方法會檢查是否調用的是list方法,若是則會使用Hibernate Criteria.setProjection(Projections.rowCount())方法,根據條件查詢總數。想深入了解可以看看HibernateCriteriaBuilder.java源碼。
PagedResultList類除了實現List接口外,添加了totalCount屬性即記錄總數,然后view層max和offset參數來控制分頁就可以了,非常的方便
等有空再說說Grails Security結合Named URL Mappings功能簡化Requestmap配置的問題
已有 3 人發表留言,猛擊->>這里<<-參與討論
ITeye推薦
一、Controlller中params
Controlller中params是grails框架中的GrailsParameterMap類,繼承自TypeConvertingMap,而不是一個簡單的Map,
除了支持普通的Map方法以外,還有其他幾個方法非常有用
Integer int(String name); Long long(String name); Double double(String name); Short(String name); List list(String name);
若需要得到數值類型的參數就非常方便了
int max= params.int("max")?:10;
二、分頁
其實使用Grails做分頁功能是最easy的事情,因為Domain類的Criteria的list方法返回的結果就是帶有分頁所用信息的PagedResultList類
Domain的動態方法會檢查是否調用的是list方法,若是則會使用Hibernate Criteria.setProjection(Projections.rowCount())方法,根據條件查詢總數。想深入了解可以看看HibernateCriteriaBuilder.java源碼。
public class HibernatePluginSupport { private static addQueryMethods(GrailsDomainClass dc, GrailsApplication application, ApplicationContext ctx) { ...; metaClass.static.createCriteria = {-> new HibernateCriteriaBuilder(domainClassType, sessionFactory)} ...; } } //groovy 動態機制 public class HibernateCriteriaBuilder { public Object invokeMethod(String name, Object obj){ createCriteriaInstance(); // 檢查分頁參數,一個參數是Map,包含分頁參數 if(name.equals(LIST_CALL) && args.length == 2) { paginationEnabledList = true; orderEntries = new ArrayList<Order>(); invokeClosureNode(args[1]); } else { invokeClosureNode(args[0]); } ... if(paginationEnabledList) { this.criteria.setFirstResult(0); this.criteria.setMaxResults(Integer.MAX_VALUE); this.criteria.setProjection(Projections.rowCount()); int totalCount = ((Integer)this.criteria.uniqueResult()).intValue(); // Drop the projection, add settings for the pagination parameters, // and then execute the query. this.criteria.setProjection(null); for(Iterator<Order> it = orderEntries.iterator();it.hasNext();){ this.criteria.addOrder(it.next()); } this.criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); GrailsHibernateUtil.populateArgumentsForCriteria(targetClass, this.criteria, (Map)args[0]); PagedResultList pagedRes = new PagedResultList(this.criteria.list()); // Updated the paged results with the total number of records // calculated previously. pagedRes.setTotalCount(totalCount); result = pagedRes; } } }
PagedResultList類除了實現List接口外,添加了totalCount屬性即記錄總數,然后view層max和offset參數來控制分頁就可以了,非常的方便
//params已有order、sort、max、offset的分頁排序信息 params.max = Math.min(params.int('max') ?: 15, 100) def criteria = CellPhoneModel.createCriteria(); def pageList = criteria.list(params, { if(params['factory.id']) factory { eq("id",params.long('factory.id')) } if(params.keyword) like("abbreviateName","%${params.keyword}%") });
等有空再說說Grails Security結合Named URL Mappings功能簡化Requestmap配置的問題
已有 3 人發表留言,猛擊->>這里<<-參與討論
ITeye推薦