您的位置:首页 > 编程语言 > Java开发

基于Spring+Hibernate+Struts框架分页的一种实现

2006-01-06 18:03 573 查看
我的办法是让实现分页的类继承HibernateDaoSupport这个类,然后才能利用这个类的 getHibernateTemplate().find()方法查询得到的结果集与hibernate的Query类的setFirstResult()和setMaxResults()去实现。核心代码如下:
接口里的方法主要就是根据传入参数拼成查询条件,增加一个页的参数page
public interface GetServiceCase {
public Pages getPages();
public Collection getServiceCaseByPage(String keyword, String createDate,
String statusId, String typeId, String id, String operatorId,
String area, String page);
}
具体实现类
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
public class GetServiceCaseImpl extends HibernateDaoSupport implements
GetServiceCase {
Pages thepages = null;//这个类属性用来保存页信息
public Collection getServiceCaseByPage(String keyword, String createDate,
String statusId, String typeId, String id, String operatorId,
String area, String page) {
boolean whereAdded = false;
String hql = "from ServiceCase scase ";
if (keyword != null && !keyword.trim().equals("")) {
hql = hql + " where scase.name = /'" + keyword + "/' ";
whereAdded = true;
}
if (createDate != null && !createDate.trim().equals("")) {
if (whereAdded) {
hql = hql + " and ";
} else {
hql = hql + " where ";
whereAdded = true;
}
hql = hql + " to_char(scase.createDate, /'yyyy-mm-dd/') = /'"
+ createDate + "/' ";
}
if (statusId != null && !statusId.trim().equals("")) {
if (whereAdded) {
hql = hql + " and ";
} else {
hql = hql + " where ";
whereAdded = true;
}
hql = hql + " scase.status.id = " + statusId;
}
if (typeId != null && !typeId.trim().equals("")) {
if (whereAdded) {
hql = hql + " and ";
} else {
hql = hql + " where ";
whereAdded = true;
}
hql = hql + " scase.type.id = " + typeId;
}
if (id != null && !id.trim().equals("")) {
if (whereAdded) {
hql = hql + " and ";
} else {
hql = hql + " where ";
whereAdded = true;
}
hql = hql + " scase.id = " + id;
}
if (operatorId != null && !operatorId.trim().equals("")) {
if (whereAdded) {
hql = hql + " and ";
} else {
hql = hql + " where ";
whereAdded = true;
}
hql = hql + " scase.worker.id = " + operatorId;
}
if (area != null && !area.trim().equals("")) {
if (whereAdded) {
hql = hql + " and ";
} else {
hql = hql + " where ";
whereAdded = true;
}
hql = hql + " scase.area = /'" + area + "/' ";//查询语句
}
//System.out.println("the sql is :" + hql);
int ipage = 1;
try {
ipage = Integer.parseInt(page);
} catch (NumberFormatException e) {//对传入的page做好防备
ipage = 1;
e.printStackTrace();
}
int startRow = 0;//开始行
int totalpage = 1;//总页数
int totalrecord = 0;//总记录数
int pagesize = 20;//页的大小
List list = new ArrayList();
try {
list = getHibernateTemplate().find(hql);//这个方法返回list
} catch (DataAccessException e) {
e.printStackTrace();
}
if (list.size() > 0) {
totalrecord = list.size();
//System.out.println(" get record size = " + list.size());
}else{
return null;//没有记录就直接返回
}
if(totalrecord != 0 && totalrecord < pagesize){
pagesize = totalrecord;
}
if (totalrecord % pagesize == 0) {
totalpage = totalrecord / pagesize;
} else {
totalpage = totalrecord / pagesize + 1;
}
//System.out.println("pagesize = " + pagesize);
//System.out.println("totalpage = " + totalpage);
if (ipage > totalpage) {
ipage = totalpage;
}
if (ipage < 1) {
ipage = 1;
}
if (ipage % totalpage == 0) {
startRow = (ipage - 1) * pagesize;
pagesize = totalrecord - startRow;//若记录不足一页,页大小以实际为准
} else {
startRow = (ipage - 1) * pagesize;//设置开始行
}
thepages = new Pages();
thepages.setThepage(String.valueOf(ipage));
thepages.setTotalpage(String.valueOf(totalpage));
try {
Query query = null;
Session session = getSession();
query = session.createQuery(hql);
query.setFirstResult(startRow);
query.setMaxResults(pagesize);
list = query.list();
return list;
} catch (HibernateException e) {
e.printStackTrace();
}
return null;
}
public Pages getPages() {
return thepages;
}
}

保存页信息的类
public class Pages {
String thepage;
String totalpage;
/**
* @return Returns the thepage.
*/
public String getThepage() {
return thepage;
}
/**
* @param thepage The thepage to set.
*/
public void setThepage(String thepage) {
this.thepage = thepage;
}
/**
* @return Returns the totalpage.
*/
public String getTotalpage() {
return totalpage;
}
/**
* @param totalpage The totalpage to set.
*/
public void setTotalpage(String totalpage) {
this.totalpage = totalpage;
}
}
action中的部分代码

public ActionForward process(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) {
DynaActionForm form = (DynaActionForm) arg1;
if (doAction != null && !doAction.equals("")) { // 提交动作
form.set("doAction", null);
String keyword = (String)form.get("keyword");
String createDate = (String)form.get("createDate");
String statusId = (String)form.get("statusId");
String typeId = (String)form.get("typeId");
String id = (String)form.get("id");
String operatorId = (String)form.get("operatorId");
String area = (String)form.get("area");
GetServiceCase getServiceCase = (GetServiceCase)SpringServiceUtil.getServiceObject(arg2,"GetServiceCase");
Collection list = getServiceCase.getServiceCaseByPage(keyword,
createDate, statusId, typeId, id, operatorId, area,page);
if(list != null){
Pages apage = getServiceCase.getPages();
arg2.setAttribute("page",apage.getThepage());
arg2.setAttribute("totalpage",apage.getTotalpage());
arg2.setAttribute("case_list", list);
arg2.setAttribute("keyword",keyword);
arg2.setAttribute("createDate",createDate);
arg2.setAttribute("statusId",statusId);
arg2.setAttribute("typeId",typeId);
arg2.setAttribute("id",id);
arg2.setAttribute("operatorId",operatorId);
arg2.setAttribute("area",area);
}
return arg0.findForward("search_result");
} else { // 初始化页面
return arg0.findForward("init");
}
}

JSP中的部分实现
<logic:present name="case_list">
<%
int thepage = Integer.parseInt((String)request.getAttribute("page"));
int tpage = Integer.parseInt((String)request.getAttribute("totalpage"));
%>
<p>
第<%=thepage%>页 
页数:<font color="red"><bean:write name="page"/>/<bean:write name="totalpage"/></font>
共<bean:write name="totalpage"/>页  
<logic:notEqual name="page" value="1">
<a href="<%=request.getContextPath()%>/case/search.do?doAction=1&keyword=<bean:write name="keyword"/>&createDate=<bean:write name="createDate"/>&statusId=<bean:write name="statusId"/>&typeId=<bean:write name="typeId"/>&id=<bean:write name="id"/>&operatorId=<bean:write name="operatorId"/>&area=<bean:write name="area"/>&page=1" >首页</a>
<a href="<%=request.getContextPath()%>/case/search.do?doAction=1&keyword=<bean:write name="keyword"/>&createDate=<bean:write name="createDate"/>&statusId=<bean:write name="statusId"/>&typeId=<bean:write name="typeId"/>&id=<bean:write name="id"/>&operatorId=<bean:write name="operatorId"/>&area=<bean:write name="area"/>&page=<%=thepage - 1%>" >上一页</a>
</logic:notEqual>
<%
if(thepage != tpage){
%>
<a href="<%=request.getContextPath()%>/case/search.do?doAction=1&keyword=<bean:write name="keyword"/>&createDate=<bean:write name="createDate"/>&statusId=<bean:write name="statusId"/>&typeId=<bean:write name="typeId"/>&id=<bean:write name="id"/>&operatorId=<bean:write name="operatorId"/>&area=<bean:write name="area"/>&page=<%=thepage + 1%>" >下一页</a>
<a href="<%=request.getContextPath()%>/case/search.do?doAction=1&keyword=<bean:write name="keyword"/>&createDate=<bean:write name="createDate"/>&statusId=<bean:write name="statusId"/>&typeId=<bean:write name="typeId"/>&id=<bean:write name="id"/>&operatorId=<bean:write name="operatorId"/>&area=<bean:write name="area"/>&page=<bean:write name="totalpage" />" >尾页</a>
<%
}
%>
输入页数:<input type="text" name="page" size="3"/>
<input type=submit name="doAction" value="go">
<input type=hidden name="keyword" value="<bean:write name="keyword"/>">
<input type=hidden name="createDate" value="<bean:write name="createDate"/>">
<input type=hidden name="statusId" value="<bean:write name="statusId"/>">
<input type=hidden name="typeId" value="<bean:write name="typeId"/>">
<input type=hidden name="id" value="<bean:write name="id"/>">
<input type=hidden name="operatorId" value="<bean:write name="operatorId"/>">
<input type=hidden name="area" value="<bean:write name="area"/>">
</logic:present>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: