S2SH分頁

 
  1. package domain;  
  2. import java.io.Serializable;  
  3. import java.sql.Date;  
  4. public class TbUser implements Serializable {  
  5.     private Integer id;  
  6.     private String name;  
  7.     private String gender;  
  8.     private Integer age;  
  9.     private Date birthday;  
  10.     private String phone;  
  11.     private String address;  
  12.     private String username;  
  13.     private String password;  
  14.       
  15.     /*省略get(),set()*/  
  16. }  

TbUser.hbm.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <hibernate-mapping package="domain">  
  5.     <class name="TbUser" table="tb_user">  
  6.         <id name="id" type="java.lang.Integer">  
  7.             <column name="id" />  
  8.             <generator class="native" />  
  9.         </id>  
  10.         <property name="name" type="java.lang.String">  
  11.             <column name="name" length="20"/>  
  12.         </property>  
  13.         <property name="gender" type="java.lang.String">  
  14.             <column name="gender" length="6"/>  
  15.         </property>  
  16.         <property name="age" type="java.lang.Integer">  
  17.             <column name="age"/>  
  18.         </property>  
  19.         <property name="birthday" type="java.sql.Date">  
  20.             <column name="birthday"/>  
  21.         </property>  
  22.         <property name="phone" type="java.lang.String">  
  23.             <column name="phone" length="11"/>  
  24.         </property>  
  25.         <property name="address" type="java.lang.String">  
  26.             <column name="address" length="50"/>  
  27.         </property>  
  28.         <property name="username" type="java.lang.String">  
  29.             <column name="username" length="20" not-null="true" />  
  30.         </property>  
  31.         <property name="password" type="java.lang.String">  
  32.             <column name="password" length="20" not-null="true" />  
  33.         </property>  
  34.     </class>  
  35. </hibernate-mapping>  

  1. package dao;  
  2. import org.hibernate.HibernateException;  
  3. import org.hibernate.Session;  
  4. import org.hibernate.cfg.Configuration;  
  5. public class HibernateSessionFactory {  
  6.     private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";  
  7.     private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();  
  8.     private  static Configuration configuration = new Configuration();  
  9.     private static org.hibernate.SessionFactory sessionFactory;  
  10.     private static String configFile = CONFIG_FILE_LOCATION;  
  11.     static {  
  12.         try {  
  13.             configuration.configure(configFile);  
  14.             sessionFactory = configuration.buildSessionFactory();  
  15.         } catch (Exception e) {  
  16.             System.err  
  17.                     .println("%%%% Error Creating SessionFactory %%%%");  
  18.             e.printStackTrace();  
  19.         }  
  20.     }  
  21.     private HibernateSessionFactory() {  
  22.     }  
  23.       
  24.     public static Session getSession() throws HibernateException {  
  25.         Session session = (Session) threadLocal.get();  
  26.         if (session == null || !session.isOpen()) {  
  27.             if (sessionFactory == null) {  
  28.                 rebuildSessionFactory();  
  29.             }  
  30.             session = (sessionFactory != null) ? sessionFactory.openSession()  
  31.                     : null;  
  32.             threadLocal.set(session);  
  33.         }  
  34.         return session;  
  35.     }  
  36.     public static void rebuildSessionFactory() {  
  37.         try {  
  38.             configuration.configure(configFile);  
  39.             sessionFactory = configuration.buildSessionFactory();  
  40.         } catch (Exception e) {  
  41.             System.err  
  42.                     .println("%%%% Error Creating SessionFactory %%%%");  
  43.             e.printStackTrace();  
  44.         }  
  45.     }  
  46.     public static void closeSession() throws HibernateException {  
  47.         Session session = (Session) threadLocal.get();  
  48.         threadLocal.set(null);  
  49.         if (session != null) {  
  50.             session.close();  
  51.         }  
  52.     }  
  53.     public static org.hibernate.SessionFactory getSessionFactory() {  
  54.         return sessionFactory;  
  55.     }  
  56.     public static void setConfigFile(String configFile) {  
  57.         HibernateSessionFactory.configFile = configFile;  
  58.         sessionFactory = null;  
  59.     }  
  60.     public static Configuration getConfiguration() {  
  61.         return configuration;  
  62.     }  
  63. }  

  1. package dao;  
  2. import util.Page;  
  3. import domain.TbUser;  
  4. public interface UserDAO {  
  5.       
  6.     public TbUser findByUsernamePassword(String username,String password);  
  7.       
  8.     public Page findByPageNo(int pageNo,int pageSize);  
  9. }  

  1. package dao;  
  2. import org.hibernate.Query;  
  3. import org.hibernate.Session;  
  4. import util.Page;  
  5. import domain.TbUser;  
  6. public class UserDAOImpl implements UserDAO {  
  7.     private Session session = HibernateSessionFactory.getSession();  
  8.     public Page findByPageNo(int pageNo, int pageSize) {  
  9.         Page page = new Page();  
  10.         Query query = session.createQuery("select count(*) from TbUser");  
  11.         int count = Integer.parseInt(query.uniqueResult().toString());  
  12.           
  13.         query = session.createQuery("from TbUser");  
  14.         query.setMaxResults(pageSize);  
  15.         query.setFirstResult((pageNo-1)*pageSize);  
  16.           
  17.         page.setList(query.list());  
  18.         page.setTotal(count);  
  19.         page.setPageNo(pageNo);  
  20.         page.setPageSize(pageSize);  
  21.           
  22.         return page;  
  23.     }  
  24.     public TbUser findByUsernamePassword(String username, String password) {  
  25.         String hql = "from TbUser where username=? and password=?";  
  26.         Query query = session.createQuery(hql);  
  27.         query.setParameter(0, username);  
  28.         query.setParameter(1, password);  
  29.         return (TbUser) query.uniqueResult();  
  30.     }  
  31. }  

  1. package service;  
  2. import util.Page;  
  3. import domain.TbUser;  
  4. public interface UserService {  
  5.       
  6.     public TbUser findByUsernamePassword(String username,String password);  
  7.       
  8.     public Page findByPageNo(int pageNo,int pageSize);  
  9. }  

  1. package service;  
  2. import util.Page;  
  3. import dao.UserDAO;  
  4. import domain.TbUser;  
  5. public class UserServiceImpl implements UserService {  
  6.     private UserDAO userDAO;  
  7.       
  8.     public Page findByPageNo(int pageNo, int pageSize) {  
  9.         return userDAO.findByPageNo(pageNo, pageSize);  
  10.     }  
  11.     public TbUser findByUsernamePassword(String username, String password) {  
  12.         return userDAO.findByUsernamePassword(username, password);  
  13.     }  
  14.     public void setUserDAO(UserDAO userDAO) {  
  15.         this.userDAO = userDAO;  
  16.     }  
  17.     public UserDAO getUserDAO() {  
  18.         return userDAO;  
  19.     }  
  20. }  

  1. package util;  
  2. import java.util.List;  
  3. public class Page {  
  4.     private int total;  
  5.     private int pageSize;  
  6.     private int totalPage;  
  7.     private int pageNo;  
  8.     private int prePage;  
  9.     private int nextPage;  
  10.     private List list;  
  11.       
  12.     public int getTotal() {  
  13.         return total;  
  14.     }  
  15.     public void setTotal(int total) {  
  16.         this.total = total;  
  17.     }  
  18.     public int getPageSize() {  
  19.         return pageSize;  
  20.     }  
  21.     public void setPageSize(int pageSize) {  
  22.         this.pageSize = pageSize;  
  23.     }  
  24.     public int getTotalPage() {  
  25.         if(total%pageSize == 0){  
  26.             totalPage = total/pageSize;  
  27.         }else{  
  28.             totalPage = total/pageSize + 1;  
  29.         }  
  30.         return totalPage;  
  31.     }  
  32.     public void setTotalPage(int totalPage) {  
  33.         this.totalPage = totalPage;  
  34.     }  
  35.     public int getPageNo() {  
  36.         return pageNo;  
  37.     }  
  38.     public void setPageNo(int pageNo) {  
  39.         this.pageNo = pageNo;  
  40.     }  
  41.     public int getPrePage() {  
  42.         if(pageNo == 1){  
  43.             prePage = pageNo;  
  44.         }else{  
  45.             prePage = pageNo - 1;  
  46.         }  
  47.         return prePage;  
  48.     }  
  49.     public void setPrePage(int prePage) {  
  50.         this.prePage = prePage;  
  51.     }  
  52.     public int getNextPage() {  
  53.         if(pageNo == totalPage){  
  54.             nextPage = pageNo;  
  55.         }else{  
  56.             nextPage = pageNo + 1;  
  57.         }  
  58.         return nextPage;  
  59.     }  
  60.     public void setNextPage(int nextPage) {  
  61.         this.nextPage = nextPage;  
  62.     }  
  63.     public List getList() {  
  64.         return list;  
  65.     }  
  66.     public void setList(List list) {  
  67.         this.list = list;  
  68.     }  
  69. }  

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.     <bean id="sessionFactory"  
  7.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  8.         <property name="configLocation"  
  9.             value="classpath:hibernate.cfg.xml" />  
  10.     </bean>  
  11.     <bean id="userDAO" class="dao.UserDAOImpl"/>  
  12.       
  13.     <bean id="userService" class="service.UserServiceImpl">  
  14.         <property name="userDAO">  
  15.             <ref local="userDAO"/>  
  16.         </property>  
  17.     </bean>  
  18.       
  19.     <bean id="loginAction" class="action.UserAction">  
  20.         <property name="userService">  
  21.             <ref local="userService"/>  
  22.         </property>  
  23.     </bean>  
  24. </beans>  

hibernate.cfg.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5. <hibernate-configuration>  
  6.     <session-factory>  
  7.         <property name="connection.username">root</property>  
  8.         <property name="connection.url">jdbc:mysql://localhost:3306/mydb</property>  
  9.         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>  
  10.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  11.         <property name="connection.password">1101</property>  
  12.         <property name="show_sql">true</property>  
  13.         <property name="format_sql">true</property>  
  14.         <property name="hbm2ddl.auto">update</property>  
  15.           
  16.         <mapping resource="domain/TbUser.hbm.xml"/>  
  17.           
  18.     </session-factory>  
  19. </hibernate-configuration>  

native_zh_CN.properties

  1. user.username=/u7528/u6237/u540d  
  2. user.password=/u5bc6/u7801  
  3. login=/u767b/u5f55  

struts.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.     <constant name="struts.ui.theme" value="simple"/>  
  7.     <constant name="struts.custom.i18n.resources" value="native"/>  
  8.     <constant name="struts.objectFactory" value="spring"/>  
  9.     <package name="user" namespace="/user" extends="struts-default">  
  10.         <action name="*" class="loginAction" method="{1}">  
  11.             <result name="success">/index.jsp</result>  
  12.             <result name="input">/login.jsp</result>  
  13.             <result name="browse">/index.jsp</result>  
  14.         </action>  
  15.     </package>  
  16. </struts>  

 

login.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib prefix="s" uri="/struts-tags" %>      
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>登錄</title>  
  9. </head>  
  10. <body>  
  11. <fieldset>  
  12.     <legend>系統登錄</legend>  
  13.     <s:form action="user/login" method="post">  
  14.         <s:text name="user.username"/><s:textfield name="username" key="user.username" label="%{getText('user.username')}"/>  
  15.         <s:text name="user.password"/><s:password name="password" key="user.password" label="%{getText('user.password')}"/>  
  16.         <s:submit key="login"/>  
  17.     </s:form>  
  18. </fieldset>  
  19. </body>  
  20. </html>  

index.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>      
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  8. <title>首頁</title>  
  9. </head>  
  10. <body>  
  11. ${message }<hr/>  
  12. <a href="user/browse" mce_href="user/browse">用戶列表</a><br/>  
  13. <table>  
  14.     <tr>  
  15.         <th>ID</th>  
  16.         <th>姓名</th>  
  17.         <th>性別</th>  
  18.         <th>年齡</th>  
  19.         <th>生日</th>  
  20.         <th>電話</th>  
  21.         <th>地址</th>  
  22.     </tr>  
  23.     <c:forEach items="${userList}" var="user">  
  24.         <tr>  
  25.             <td>${user.id }</td>  
  26.             <td>${user.name }</td>  
  27.             <td>${user.gender }</td>  
  28.             <td>${user.age }</td>  
  29.             <td>${user.phone }</td>  
  30.             <td>${user.address }</td>  
  31.         </tr>  
  32.     </c:forEach>  
  33. </table>  
  34. 共${page.totalPage }頁|當前第${page.pageNo }頁|共${page.total }條記錄<br/>  
  35. <!-- 分頁列表 -->  
  36. <c:if test="${page.totalPage != 1}">  
  37.     <!-- 首頁情況 -->  
  38.     <c:if test="${page.pageNo == 1}">  
  39.         <font color="gray">首頁  上一頁</font>  
  40.         <a href="user/browse?pageNo=${page.nextPage }" mce_href="user/browse?pageNo=${page.nextPage }">下一頁</a>  
  41.         <a href="user/browse?pageNo=${page.totalPage }" mce_href="user/browse?pageNo=${page.totalPage }">尾頁</a>  
  42.     </c:if>  
  43.     <!-- 尾頁情況 -->  
  44.     <c:if test="${page.pageNo == page.totalPage}">  
  45.         <a href="user/browse?pageNo=1" mce_href="user/browse?pageNo=1">首頁</a>  
  46.         <a href="user/browse?pageNo=${page.prePage }" mce_href="user/browse?pageNo=${page.prePage }">上一頁</a>  
  47.         <font color="gray">下一頁   尾頁</font>    
  48.     </c:if>  
  49.     <!-- 普通情況 -->  
  50.     <c:if test="${page.pageNo!=1 and page.pageNo!=page.totalPage}">  
  51.         <a href="user/browse?pageNo=1" mce_href="user/browse?pageNo=1">首頁</a>  
  52.         <a href="user/browse?pageNo=${page.prePage }" mce_href="user/browse?pageNo=${page.prePage }">上一頁</a>  
  53.         <a href="user/browse?pageNo=${page.nextPage }" mce_href="user/browse?pageNo=${page.nextPage }">下一頁</a>  
  54.         <a href="user/browse?pageNo=${page.totalPage }" mce_href="user/browse?pageNo=${page.totalPage }">尾頁</a>  
  55.     </c:if>  
  56. </c:if>  
  57. <!-- 分頁列表 -->  
  58. </body>  
  59. </html>  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章