




已阅读5页,还剩17页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1). 针对网上一些 struts2分页的例子 不完整 ,代码混乱繁琐 ,我特意写了一个分页的小例子,供大家参考,如果程序有什么纰漏, 请指出。开发环境:eclipse3.2 + myeclipse5.5mysql5.0tomcat5.5Dreamweaver20042)建库,并导入脚本这里给大家介绍一个mysql客户端工具 - SQLyog ,非常好用。-admin.sql-/*SQLyog Community Edition- MySQL GUI v6.12MySQL - 5.0.37-community-nt : Database - test把这段脚本导入到mysql的test库里面(你可以先建一个test库 ,这样会省去很多麻烦)*/*!40101 SET NAMES utf8 */;/*!40101 SET SQL_MODE=*/;create database if not exists test;USE test;/*!40014 SET OLD_FOREIGN_KEY_CHECKS=FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;/*!40101 SET OLD_SQL_MODE=SQL_MODE, SQL_MODE=NO_AUTO_VALUE_ON_ZERO */;/*Table structure for table admin */DROP TABLE IF EXISTS admin;CREATE TABLE admin (id int(11) NOT NULL auto_increment,user char(20) default NULL,pass char(20) default NULL,PRIMARY KEY (id) ENGINE=InnoDB DEFAULT CHARSET=gb2312;/*Data for the table admin */insert into admin(id,user,pass) values (786432,天使,#),(786433,天使,#),(786434,天使,#),(786435,天使,#),(786436,天使,#),(819200,天使,#),(819201,天使,#),(819202,天使,#),(819203,天使,#),(819204,天使,#),(819205,wangbacheng,#),(851968,天使,#),(851969,天使,#),(851970,天使,#),(851971,天使,#),(851972,天使,#);/*!40101 SETSQL_MODE=OLD_SQL_MO*/;/*!40014 SETFOREIGN_KEY_CHECKS=OLD_FOREIGN_KEY_CHECKS*/;DE3)建立数据库连接程序 ,也就是hibernate相关程序,都可以用eclipse自动生成,这个不用多说在eclipse 中只要引入 hibernate核心包和struts2 包 就可以了-HibernateSessionFactory.java-package action;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.cfg.Configuration;/* Configures and provides access to Hibernate sessions, tied to the* current thread of execution. Follows the Thread Local Session* pattern, seelink/42.html.*/public class HibernateSessionFactory /* * Location of hibernate.cfg.xml file. * Location should be on the classpath as Hibernate uses * #resourceAsStream style lookup for its configuration file. * The default classpath location of the hibernate config file is * in the default package. Use #setConfigFile() to update * the location of the configuration file for the current session. */ private static String CONFIG_FILE_LOCATION = /hibernate.cfg.xml;private static final ThreadLocal threadLocal = new ThreadLocal(); private static Configuration configuration = new Configuration(); private static org.hibernate.SessionFactory sessionFactory; private static String configFile = CONFIG_FILE_LOCATION;static try configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); catch (Exception e) System.err .println(% Error Creating SessionFactory %); e.printStackTrace(); private HibernateSessionFactory() /* * Returns the ThreadLocal Session instance. Lazy initialize * the SessionFactory if needed. * * return Session * throws HibernateException */ public static Session getSession() throws HibernateException Session session = (Session) threadLocal.get(); if (session = null | !session.isOpen() if (sessionFactory = null) rebuildSessionFactory(); session = (sessionFactory != null) ? sessionFactory.openSession() : null; threadLocal.set(session); return session; /* * Rebuild hibernate session factory * */public static void rebuildSessionFactory() try configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); catch (Exception e) System.err .println(% Error Creating SessionFactory %); e.printStackTrace(); /* * Close the single hibernate session instance. * * throws HibernateException */ public static void closeSession() throws HibernateException Session session = (Session) threadLocal.get(); threadLocal.set(null); if (session != null) session.close(); /* * return session factory * */public static org.hibernate.SessionFactory getSessionFactory() return sessionFactory;/* * return session factory * * session factory will be rebuilded in the next call */public static void setConfigFile(String configFile) HibernateSessionFactory.configFile = configFile; sessionFactory = null;/* * return hibernate configuration * */public static Configuration getConfiguration() return configuration;-Admin.java-package action;/* Admin generated by MyEclipse Persistence Tools*/public class Admin implements java.io.Serializable / Fields private Integer id; private String user; private String pass; / Constructors /* default constructor */ public Admin() /* minimal constructor */ public Admin(Integer id) this.id = id; /* full constructor */ public Admin(Integer id, String user, String pass) this.id = id; this.user = user; this.pass = pass; / Property accessors public Integer getId() return this.id; public void setId(Integer id) this.id = id; public String getUser() return this.user; public void setUser(String user) this.user = user; public String getPass() return this.pass; public void setPass(String pass) this.pass = pass; -Admin.xml- -hibernate.cfg.xml-root jdbc:mysql:/localhost:3306/test org.hibernate.dialect.MySQLDialectMysqlroot com.mysql.jdbc.Driver4).建个工具类,把一些方法封装到里面-PagerHelper.java-package action;import java.util.Collection;import java.util.Iterator;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.Transaction;/* author like_dark* 注意:java包命名的时候不能以 ”java“命名,例如 java.my.xxx ; 真是奇怪啊*/public class PagerHelper public Collection findWithPage(int pageSize, int startRow) throwsHibernateException Collection admins = null; Transaction tx = null; try Session session = HibernateSessionFactory.getSession(); tx = session.beginTransaction() ; Query q = session.createQuery(from Admin); q.setMaxResults(pageSize); q.setFirstResult(startRow); admins = q.list(); mit(); catch (HibernateException he) if (tx != null) tx.rollback(); throw he; finally HibernateSessionFactory.closeSession(); return admins;public int getRows(String query) throwsHibernateException /query : select count(*) from Admin int totalRows = 0; Transaction tx = null; try Session session = HibernateSessionFactory.getSession(); tx = session.beginTransaction(); totalRows = (Long) session.createQuery(query).list().iterator().next().intValue() ; mit(); catch (HibernateException he) if (tx != null) tx.rollback(); throw he; finally HibernateSessionFactory.closeSession(); return totalRows;public static void main(String args) PagerHelper ph = new PagerHelper() ;System.out.println(rows:+ph.getRows(select count(*) from Admin) ;5) .struts2相关程序和配置-struts.xml- /index.jsp /index.jsp - PageAction.java -package action;import java.util.ArrayList;import java.util.Collection;import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class PageAction extends ActionSupport private int totalRows; /总行数private int pageSize = Constants.PAGE_SIZE; /每页显示的行数private int currentPage; /当前页号private int totalPages; /总页数private boolean hasNext ; /是否有下一页private boolean hasPrevious ; /是否有上一页private Collection CurrentList; /当前数据集private Collection indexList ; /页码集合public Collection getCurrentList() return CurrentList;public void setCurrentList(Collection currentList) CurrentList = currentList;/* 这里最好用一个引用参数化*/public Collection getIndexList() ArrayList result = new ArrayList(); for(int i = 1;i=getTotalPages();i+) result.add(new Integer(i); return result ;public void setIndexList(Collection indexList) this.indexList = indexList;public String execute() throws Exception int totalRows = 0 ; try PagerHelper ph = new PagerHelper() ; totalRows = ph.getRows(select count(*) from Admin) ; /总行数 totalPages = (totalRows + pageSize - 1) /pageSize; /总页数 setIndexList(getIndexList() ; /生成页码集合 if (currentPage=0 | currentPage totalPages) last(); if(currentPage = 1 & currentPage 1 & currentPage = totalPages) hasPrevious = true ; /* 为了利用session 可以实现SessionAware接口 然后重载 void setSession(Map session) 方法* action implementation SessionAware* private Map session ;* void setSession(Map session)* this.session = session ;* * session.put(.); session.get(.)* 或者 继承import com.opensymphony.xwork2.ActionContext;* ActionContext ctx = ActionContext.getContext(); Map session = ctx.getSession() ;*/ CurrentList = ph.findWithPage(Constants.PAGE_SIZE, getStartRow() ; return SUCCESS ; catch(Exception e) e.printStackTrace() ; return ERROR ; public int getCurrentPage() return currentPage;public void setCurrentPage(int currentPage) this.currentPage = currentPage;public int getPageSize() return pageSize;public void setPageSize(int pageSize) this.pageSize = pageSize;public int getStartRow() if(getCurrentPage() = 1) return 0; else return (getCurrentPage()-1) * pageSize;public int getTotalPages() return totalPages;public void setTotalPages(int totalPages) this.totalPages = totalPages;public int getTotalRows() return totalRows;public void setT
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 龙岩市中储粮2025秋招基建工程岗高频笔试题库含答案
- 2025年春季江苏省环保集团有限公司招聘考前自测高频考点模拟试题附答案详解(完整版)
- 国家能源平顶山市2025秋招综合管理类面试追问及参考回答
- 2025年甘肃酒泉阿克塞县人民检察院招聘聘用制人员考前自测高频考点模拟试题及答案详解(网校专用)
- 2025年蚌埠市龙子湖区产业发展有限公司招聘22人模拟试卷含答案详解
- 2025年宁波前湾新区卫生系统事业单位招聘副高及以上高层次人才2人模拟试卷附答案详解(典型题)
- 2025年移动互联网行业发展趋势与市场分析报告
- 2025年数控机床智能化升级关键技术突破与应用案例报告
- 2025年工业互联网平台联邦学习隐私保护技术在智慧园区中的应用研究报告
- 2025年石油市场供需格局变化下的全球能源市场结构调整研究报告
- 《屋顶分布式光伏电站建设规范》
- 高考英语读后续写自然景色描写升华句(风+雨+雪+霜+雾)清单
- 建筑师负责制工程建设项目建筑师标准服务内容与流程
- 九年级数学第一次月考卷 北师大版
- 《精护》第六章-精神活性物质所致精神障碍患者的护理
- 与孩子立契约协议书范本
- 姜萍事件全文课件
- 2024全国职业院校技能大赛ZZ060母婴照护赛项规程+赛题
- 特殊天气驾驶安全规范
- 新闻文体的翻译课件
- 西方翻译理论流派划分探索
评论
0/150
提交评论