iamhuzl

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            1 隨筆 :: 13 文章 :: 21 評論 :: 0 Trackbacks
          Grails小技巧
          一、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推薦



          posted on 2010-04-15 11:31 溫水青蛙 閱讀(1028) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 临猗县| 东宁县| 余庆县| 巴楚县| 南川市| 平武县| 泗阳县| 宜黄县| 互助| 兴海县| 宁远县| 咸丰县| 大安市| 中西区| 富蕴县| 莆田市| 阿拉善盟| 丹凤县| 安庆市| 安康市| 县级市| 桦南县| 石阡县| 蒙自县| 宁国市| 田林县| 汉阴县| 庆城县| 出国| 繁峙县| 辉南县| 桦甸市| 如皋市| 南召县| 隆昌县| 苍山县| 闵行区| 西丰县| 玛沁县| 泉州市| 肃南|