您的位置:首页 > 其它

wicket之实现IDataProvider接口,只查询当前页数据

2015-06-16 17:04 465 查看
1.html文件

Html代码


<html>
<head>
<link href="styles/wicket.css" rel="stylesheet" type="text/css">
<link href="styles/office.css" rel="stylesheet" type="text/css">
</head>
<body>
<h3>DataView+ListDataProvider例子</h3>
<form wicket:id ="form">
<span wicket:id ="navigator">这里显示分页操作</span>
<table width="100%" border="0" cellspacing="1" cellpadding="0" id="baseCodeSetting">
<thead>
<tr>
<th align="center">姓名</th>
<th align="center" width="50">性别</th>
<th align="center">职务</th>
<th align="center">联系电话</th>
<th align="center">QQ</th>
<th align="center">E-mail</th>
<th align="center">博客地址</th>
<th align="center" width="50">操作</th>
</tr>
</thead>
<tbody>
<tr wicket:id="cards">
<td align="center" wicket:id ="name">title </td>
<td align="center" wicket:id ="sex">author</td>
<td align="center" wicket:id ="duty">duty</td>
<td align="center" wicket:id ="tel">tel</td>
<td align="center" wicket:id ="qq">qq</td>
<td align="center" wicket:id ="e_mail">e_mail</td>
<td align="center"><a wicket:id ="blog">blog</a></td>
<td align="center"><a wicket:id="edit">edit</a></td>
</tr>
</tbody>
</table >
</form>
</body>
</html>

2.java文件

Java代码


package com.logcd.wicket.cards;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.logcd.web.bo.KingCards;
import com.logcd.web.bo.dao.KingCardsDao;

@SuppressWarnings({ "unchecked", "serial" })
public class DataViewPage extends WebPage{

@SpringBean(name = "kingCardsDao")
private KingCardsDao kingCardsDao;

public DataViewPage() {
super();
final DataView listView = new DataView("cards", new KingCardsDataProvider(), 2) {
protected void populateItem(Item item) {
final KingCards card = (KingCards) item.getModelObject();
item.add(new Label("name", card.getName()));
item.add(new Label("sex", card.getSex()));
item.add(new Label("duty", card.getDuty()));
item.add(new Label("tel", card.getTel()));
item.add(new Label("e_mail", card.getEMail()));
item.add(new Label("qq", card.getQq()));
item.add(new ExternalLink("blog", card.getBlog(),card.getBlog()));
item.add(new Link("edit") {
public void onClick() {
}
});
}
};
Form form = new Form("form") {
protected void onSubmit() {
}
};
this.add(form);
form.add(listView);
form.add(new PagingNavigator("navigator", listView));
}

/**
* 提供分页数据的内部类
* @author logcd
*/
public class KingCardsDataProvider implements IDataProvider{
private static final long serialVersionUID = 1L;
private List list = Collections.synchronizedList(new ArrayList());

public Iterator iterator(int firstItem, int itemsPerPage) {
Integer pageNo=firstItem/itemsPerPage;//查询第几页
StringBuilder hql = new StringBuilder();
hql.append("from KingCards cards where cards.Type=0");
list=kingCardsDao.findOnePageObjects(hql.toString(), pageNo, itemsPerPage);
return list.iterator();
}

public IModel model(Object obj) {
return new Model((Serializable) obj);
}

public int size() {
return kingCardsDao.findRowCount("select count(*) from KingCards cards where cards.Type=0");
}

public void detach() {
this.list.clear();
}
}
}

3.dao中的两个方法

Java代码


/**
* 根据Hql,返回结果集中的一页
* @param hql
* @param pageNo 第几页
* @param itemsPerPage 每页记录条数
* @return
*/
@SuppressWarnings("unchecked")
public List<T> findOnePageObjects(final String hql,final Integer pageNo,final Integer itemsPerPage){
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return session.createQuery(hql).setFirstResult(pageNo*itemsPerPage).setMaxResults(itemsPerPage).list();
}
});
}
/**
* 根据自己的制定的hql来查询返回的所有记录的总数
*
* @param hql
* @return
*/
public int findRowCount(final String hql) {
return ((Long) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return session.createQuery(hql).iterate().next();
}

})).intValue();
}

转自: http://log-cd.iteye.com/blog/336472
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: