ソート機能付きListActionの書き方
1463 ワード
@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の使用方法に注意し、sb="ss"ではなく直接appendを得ることができる.append(..)