有排序功能的ListAction的写法

@Override
	public String execute() throws Exception {
		// this.items = this.service.getDocumentItemsByCatalogId(catalogId);

		//为如果没有点击排序图标而是通过跳转到列表页面时设置默认情况
		if(property==null){
			property="createDate";
			way="desc";
		}
		this.items = this.service.getDocumentItemsByCatalogIdAndcondition(catalogId, property, way);
		
		for (DocumentItem item : items) {
			item.setIconType(this.service.getIconType(item.getType()));
		}
		return SUCCESS;
	}

 因为items的查找是通过传排序条件来得到的,而删除,更新等操作完成后一般也会转到这个页面,但如果让它们在传过来的过程中把这些条件的属性值也传过来就不太合适了,这时可以使用上面的方法,当这些属性值为空时就给它们设定相应的默认值就可以了,且可以同时只使用同一个方法来进行所有的列表操作,上面的property指排序的列名,如该查询方法的实现为:

@SuppressWarnings("unchecked")
	public List<DocumentItem> getDocumentItemsByCatalogIdAndcondition(long catalogId, String property, String way) {
		StringBuffer sb = new StringBuffer();
		sb.append("from DocumentItem bean where bean.catalog.id=? order by bean.").append(property).append(" ").append(way);
		List<DocumentItem> list =(List<DocumentItem>) getHibernateTemplate().find(sb.toString(),catalogId);
		return list;
	}

 上面的方法中要注意hql语句不可以为 order by bean.?这样来传参数,所以这就是为什么使用StringBuffer来拼凑字符串了,注意StringBuffer的使用方法,得到一个实例后就可以直接append,而不是sb="ss".append(..)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值