- 注册时间
- 2011-5-16
- 最后登录
- 2021-1-13
- 阅读权限
- 200
- 积分
- 133067
- 精华
- 1
- 帖子
- 154
|
第六步:在包com.dao包建立整个程序的do层;文件名为:softDao.java
代码如下:
package com.dao;
/**
*
* @author Even
*/
import com.Hibernate.NewHibernateUtil;
import com.bean.soft;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
public class softDao {
public List<soft> queryByPage(int pageSize, int pageNow, String HQL) {
Session session = null;
List sftlist = new ArrayList();
try {
session = NewHibernateUtil.getsession();// 获得session对象
//String hql = "from Employee emp";// 查询HQL语句
HQL = "from soft sft";// 条件查询HQL语句
Query q = session.createQuery(HQL);// 执行查询操作
q.setFirstResult(pageSize * (pageNow - 1));
q.setMaxResults(pageSize);
sftlist = q.list();
} catch (HibernateException e) {
e.printStackTrace();
System.out.println("查询失败");
} finally {
NewHibernateUtil.closesessicon();// 关闭session
}
return sftlist;
}
//获得总页数的方法有时间可能会单独使用该工程所以将获得session的过程也单独写出来
public int getpageCount(int pagesize, String HQL) {
int pageCount;
int Datacount = 0;
Session session = null;
try {
session = NewHibernateUtil.getsession();// 获得session对象
HQL = "from soft sft";//条件查询HQL语句,这里注意使用的实例查询方式,soft是我们建立的javabean
Query q = session.createQuery(HQL);// 执行查询操作
Datacount = q.list().size();//获得记录总数
} catch (HibernateException e) {
e.printStackTrace();
System.out.println("查询失败");
} finally {
NewHibernateUtil.closesessicon();// 关闭session
}
if (Datacount % pagesize == 0) {
pageCount = Datacount / pagesize;
} else {
pageCount = Datacount / pagesize + 1;
}
return pageCount;
}
}
时间有点晚了所以这里就不多解释了。
第七步:在包com.action 建立分页的Action文件名为:softlistAction.java
代码如下:
package com.action;
import com.bean.leavetalk;
import com.bean.soft;
import com.dao.Dao;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class softlistAction extends ActionSupport {
private List<soft> softs;//用于数据集合对象并非只是软件
private List<soft> downcountlist;//用于显示下载列表的集合。
private List<leavetalk> leavatalks;//用于显示用户留言列表的集合。
private int pageNow = 1; //初始化为1,默认从第一页开始显示
private int pageSize = 4; //每页显示5条记录
private int pageCount;//总页数
private String doing;//标记Action返回的值
private Dao pageDAO = new Dao();
private Dao leivetalkdao = new Dao();
private int id;
private String fbadcount;
private String fgoodcount;
public List<soft> getSofts() {
return softs;
}
public void setSofts(List<soft> softwares) {
this.softs = softs;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageCount() {
Dao sa = new Dao();
sa.getpageCount(pageSize, "");
this.pageCount = sa.getpageCount(pageSize, "");
return sa.getpageCount(pageSize, "");
}
/**
* @param pageCount the pageCount to set
*/
public void setPageCount() {
Dao sa = new Dao();
sa.getpageCount(pageSize, "");
this.pageCount = sa.getpageCount(pageSize, "");
}
/**
* @return the doing
*/
public String getDoing() {
return doing;
}
/**
* @param doing the doing to set
*/
public void setDoing(String doing) {
this.doing = doing;
}
//主方法
public String execute() throws Exception {
String returnstr = "erro";
String HQLstr = "";
// softs = pageDAO.queryByPage(pageSize, pageNow, HQLstr);
if (doing.equals("productlist")) {
returnstr = "productlist";
HQLstr = "from soft sft";
this.setPageSize(4);
this.leavatalks = leivetalkdao.getleavetalks("from leavetalk lt order by lt.id desc");
this.softs = pageDAO.queryByPage(pageSize, pageNow, HQLstr);
} else if (doing.equals("productshow")) {
returnstr = "productshow";
HQLstr = "from soft sft where sft.id='" + getId() + "' order by sft.id asc";
downcountlist = pageDAO.queryByPage(10, pageNow, "from soft sft order by sft.fDownTimes desc");
} else if (doing.equals("index")) {
returnstr = "index";
HQLstr = " from soft sft where sft.ftype='T'order by sft.id asc";
this.setPageSize(5);
} else if (doing.equals("web")) {
returnstr = "web";
HQLstr = " from soft sft where sft.ftype='T'order by sft.id asc";
this.setPageSize(5);
} else if (doing.equals("service")) {
returnstr = "service";
//HQLstr = " from service sr where order by sr.id asc";
// this.setPageSize(1);
} else if (doing.equals("Updatefgood")) {
returnstr = "Updatefgood";
HQLstr = "from soft sft where sft.id='" + getId() + "' order by sft.id asc";
downcountlist = pageDAO.queryByPage(10, pageNow, "from soft sft order by sft.fDownTimes desc");
pageDAO.UpdatefgoodAndfbad(getId(), "updategood", fgoodcount);
} else if (doing.equals("Updatefbad")) {
returnstr = "Updatefgood";
HQLstr = "from soft sft where sft.id='" + getId() + "' order by sft.id asc";
downcountlist = pageDAO.queryByPage(10, pageNow, "from soft sft order by sft.fDownTimes desc");
pageDAO.UpdatefgoodAndfbad(getId(), "updatebad", fbadcount);
} else {
returnstr = "erro";
}
softs = pageDAO.queryByPage(pageSize, pageNow, HQLstr);
System.out.println(softs.size());
return returnstr;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the downcountlist
*/
public List<soft> getDowncountlist() {
return downcountlist;
}
/**
* @param downcountlist the downcountlist to set
*/
public void setDowncountlist(List<soft> downcountlist) {
this.downcountlist = downcountlist;
}
/**
* @return the fbadcount
*/
public String getFbadcount() {
return fbadcount;
}
/**
* @param fbadcount the fbadcount to set
*/
public void setFbadcount(String fbadcount) {
Integer b = Integer.valueOf(fbadcount) + 1;
this.fbadcount = b.toString();
}
/**
* @return the fgoodcount
*/
public String getFgoodcount() {
return fgoodcount;
}
/**
* @param fgoodcount the fgoodcount to set
*/
public void setFgoodcount(String fgoodcount) {
Integer b = Integer.valueOf(fgoodcount) + 1;
this.fgoodcount = b.toString();
}
/**
* @return the leavatalks
*/
public List<leavetalk> getLeavatalks() {
return leavatalks;
}
/**
* @param leavatalks the leavatalks to set
*/
public void setLeavatalks(List<leavetalk> leavatalks) {
this.leavatalks = leavatalks;
}
}
第八步:配置ACTION 文件STRUTS.xml的代码如下:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="example.xml"/>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="softlist" class="com.action.softlistAction">
<result name="SUCCESS">test.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
第九步:建立JSP文件 来享受结果了
test.jsp代码如下:
< import="java.sql.ResultSet"%>
< import="java.util.List"%>
< contentType="text/html"%>
< import="org.apache.struts2.ServletActionContext"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<script language="javascript" type="text/javascript">
<%
String PageNow = request.getAttribute("pageNow").toString();
String pageCount = request.getAttribute("pageCount").toString();
%>
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>分页测试</title>
</head>
<table style=" border: 2px">
<s:iterator value="softs">
<td><s:property value="fSoftname"/></td>
<td><s:property value="fUsedSystem"/></td>
<td><s:property value="fUpdateTime"/></td>
</s:iterator>
</table>
<div>
<ul><li><a href='softlist.action?pageNow=1'>首页</a></li>
<li><a href='softlist.action?pageNow=<s:property value="%{PageNow-1}"/>'> 上一页</a></li>
<li><a href='softlist.action?pageNow=<s:property value="%{PageNow+1}"/>'>下一页</a></li>
<li><a href='softlist.action?pageNow=<%=pageCount%>'>末页</a></li>
<li><span class="pageinfo">第<strong><s:property value="#request.pageNow"/></strong>页</span></li>
<li><span class="pageinfo">共<strong><s:property value="#request.pageCount"/></strong>页</span></li>
</ul></div>
<body>
</body>
</html>
第十步:建立link.jsp
代码如下:
<%--
Document : link
Created on : 2011-1-24, 18:42:14
Author : Even
--%>
< contentType="text/html" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>JSP Page</title>
</head>
<body>
<h1><a href="softlist.action">单击浏览效果</a></h1>
</body>
</html>
注意:后面的演示我是重新制作的页面不是我的网站页面效果所以只是完成了一个原理而已,请高人高抬贵手 本人刚入门不到一个月的时间,利用这篇文章来回顾我自己学的东西同事也分享给大家,网上很多 STRUTS2的分页文章没有一个完整的。这里面的分页原理用的是 session里面的记录集查询中的 setFirstResult和setMaxResults,关于算法大家自己去揣摩就是了很简单。我正在写一个工作室的网站虽然用asp.net来写很快但是我觉得用熟悉JAVA的话会更快,呵呵。以后我会多写一些东西的。
|
|