wicket之实现IDataProvider接口,只查询当前页数据
2015-06-16 17:04
465 查看
1.html文件
Html代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/3944d4f12684d37ba627adfced4c250b.png)
<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代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/3944d4f12684d37ba627adfced4c250b.png)
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代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/3944d4f12684d37ba627adfced4c250b.png)
/**
* 根据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
Html代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/3944d4f12684d37ba627adfced4c250b.png)
<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代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/3944d4f12684d37ba627adfced4c250b.png)
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代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/3944d4f12684d37ba627adfced4c250b.png)
/**
* 根据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
相关文章推荐
- Java中JTextArea如何添加滚动条
- FireDAC 下的 Sqlite [3] - 获取数据库的基本信息
- NSProcessInfo类方法介绍
- Java初级笔记_面向对象3
- 浅析CentOS和RedHat Linux的区别
- chrome提示"安装unity web player"
- Linux makefile 课程 非常具体的,和理解
- Count Complete Tree Nodes
- 最新Cocos2d-x3.2开发环境搭建(windows环境下)
- [笔记]Emacs笔记:一,Windows下配置Emacs来开发Python
- 世纪互联的优势
- 计算机网络协议概况
- 启动哨兵Startup Sentinel(1.5.0.11)汉化版
- 设置iptables阻止一个网段访问服务器
- Window 通过cmd查看端口占用、相应进程、杀死进程等的命令
- 第十九讲字符数据及字符类型|字母及其编码|能对齐的数据
- 关于微信开发的语音存储问题
- 在Activity中响应ListView内部按钮的点击事件的两种方法
- FireDAC 下的 Sqlite [2] - 第一个例子
- 代码重构技术方法