这套解决方案,是按照MVC的模式来做的,基于STRUTS框架。分为三个部分,表现层用的是JSP,其中用了JSTL和pager taglib 做分页;控制层就 是用的很传统的Action;底层就用的是Spring+Hibernate。下面分三个部分慢慢说:
说到分页:
1 首先新建一个PageModel类
- package com.dengmin.oa.utils;
-
- import java.util.List;
-
- public class PageModel {
-
-
-
-
- private int total;
-
-
-
- private List datas;
-
- public List getDatas() {
- return datas;
- }
-
- public void setDatas(List datas) {
- this .datas = datas;
- }
-
- public int getTotal() {
- return total;
- }
-
- public void setTotal( int total) {
- this .total = total;
- }
- }
- package com.dengmin.oa.utils;
-
- import java.util.List;
-
- public class PageModel {
-
-
-
-
- private int total;
-
-
-
- private List datas;
-
- public List getDatas() {
- return datas;
- }
-
- public void setDatas(List datas) {
- this .datas = datas;
- }
-
- public int getTotal() {
- return total;
- }
-
- public void setTotal( int total) {
- this .total = total;
- }
- }
- package com.dengmin.oa.utils;
-
- import java.util.List;
-
- public class PageModel {
-
-
-
-
- private int total;
-
-
-
- private List datas;
-
- public List getDatas() {
- return datas;
- }
-
- public void setDatas(List datas) {
- this .datas = datas;
- }
-
- public int getTotal() {
- return total;
- }
-
- public void setTotal( int total) {
- this .total = total;
- }
- }
package com.dengmin.oa.utils;
import java.util.List;
public class PageModel {
/**
* 总记录数
*/
private int total;
/**
* 当前页的记录集
*/
private List datas;
public List getDatas() {
return datas;
}
public void setDatas(List datas) {
this.datas = datas;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
2 底层是基于Spring+Hibernate来做的。在DAO这一层,为其增加两个方法,底层的分页查询用的是Hibernate提供的分页查询。
- public interface UserDao {
-
- public void addUser(User user);
-
-
-
-
-
-
-
- public PageModel findAllUser( int offset, int pagesize);
- }
- public interface UserDao {
-
- public void addUser(User user);
-
-
-
-
-
-
-
- public PageModel findAllUser( int offset, int pagesize);
- }
- public interface UserDao {
-
- public void addUser(User user);
-
-
-
-
-
-
-
- public PageModel findAllUser( int offset, int pagesize);
- }
public interface UserDao {
public void addUser(User user);
/**
*
* @param offset 从第几条记录开始查询
* @param pagesize 每页显示多少条记录
* @return
*/
public PageModel findAllUser(int offset, int pagesize);
}
3 UserDao的实现类
- public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
-
- public void addUser(User user) {
- this .getHibernateTemplate().save(user);
- }
-
- public PageModel findAllUser( int offset, int pagesize) {
-
-
- String queryCountHql = "select count(*) from User" ;
-
- Query query = getSession().createQuery(queryCountHql);
- int total = ((Long)query.uniqueResult()).intValue();
-
- List datas = getSession().createQuery( "from User" )
- .setFirstResult(offset)
- .setMaxResults(pagesize)
- .list();
-
- PageModel pm = new PageModel();
- pm.setTotal(total);
- pm.setDatas(datas);
-
- return pm;
- }
-
- }
- public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
-
- public void addUser(User user) {
- this .getHibernateTemplate().save(user);
- }
-
- public PageModel findAllUser( int offset, int pagesize) {
-
-
- String queryCountHql = "select count(*) from User" ;
-
- Query query = getSession().createQuery(queryCountHql);
- int total = ((Long)query.uniqueResult()).intValue();
-
- List datas = getSession().createQuery( "from User" )
- .setFirstResult(offset)
- .setMaxResults(pagesize)
- .list();
-
- PageModel pm = new PageModel();
- pm.setTotal(total);
- pm.setDatas(datas);
-
- return pm;
- }
-
- }
- public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
-
- public void addUser(User user) {
- this .getHibernateTemplate().save(user);
- }
-
- public PageModel findAllUser( int offset, int pagesize) {
-
-
- String queryCountHql = "select count(*) from User" ;
-
- Query query = getSession().createQuery(queryCountHql);
- int total = ((Long)query.uniqueResult()).intValue();
-
- List datas = getSession().createQuery("from User" )
- .setFirstResult(offset)
- .setMaxResults(pagesize)
- .list();
-
- PageModel pm = new PageModel();
- pm.setTotal(total);
- pm.setDatas(datas);
-
- return pm;
- }
-
- }
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public void addUser(User user) {
this.getHibernateTemplate().save(user);
}
public PageModel findAllUser(int offset, int pagesize) {
//得到总记录数
String queryCountHql = "select count(*) from User";
Query query = getSession().createQuery(queryCountHql);
int total = ((Long)query.uniqueResult()).intValue();
List datas = getSession().createQuery("from User")
.setFirstResult(offset)
.setMaxResults(pagesize)
.list();
//得到结果集
PageModel pm = new PageModel();
pm.setTotal(total);
pm.setDatas(datas);
return pm;
}
}
ok!底层的实现工作就做好了
在控制层中UserAction从DispatchAction继承重写它的 unspecified方法
通过spring注入UserDao 代码如下:
- public class UserAction extends DispatchAction {
-
- private UserDao userDao;
- @Override
- protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) throws Exception {
-
- int offset = 0 ;
-
- try {
- offset = Integer.parseInt(request.getParameter( "pager .offset" ));
- } catch (Exception e) {
- }
-
- PageModel pm = userDao.findAllUser(offset, 10 );
- request.setAttribute( "pm" , pm);
-
- return mapping.findForward( "success" );
- }
-
- public void setUserDao(UserDao userDao) {
- this .userDao = userDao;
- }
-
- }
- public class UserAction extends DispatchAction {
-
- private UserDao userDao;
- @Override
- protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) throws Exception {
-
- int offset = 0 ;
-
- try {
- offset = Integer.parseInt(request.getParameter( "pager .offset" ));
- } catch (Exception e) {
- }
-
- PageModel pm = userDao.findAllUser(offset, 10 );
- request.setAttribute( "pm" , pm);
-
- return mapping.findForward( "success" );
- }
-
- public void setUserDao(UserDao userDao) {
- this .userDao = userDao;
- }
-
- }
- public class UserAction extends DispatchAction {
-
- private UserDao userDao;
- @Override
- protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) throws Exception {
-
- int offset = 0 ;
-
- try {
- offset = Integer.parseInt(request.getParameter("pager .offset" ));
- } catch (Exception e) {
- }
-
- PageModel pm = userDao.findAllUser(offset, 10 );
- request.setAttribute("pm" , pm);
-
- return mapping.findForward( "success" );
- }
-
- public void setUserDao(UserDao userDao) {
- this .userDao = userDao;
- }
-
- }
public class UserAction extends DispatchAction {
private UserDao userDao;
@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
int offset = 0;
try {
offset = Integer.parseInt(request.getParameter("pager
.offset"));
} catch (Exception e) {
}
//这里我按照每页显示10条
PageModel pm = userDao.findAllUser(offset, 10);
request.setAttribute("pm", pm);
return mapping.findForward("success");
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
spring中的配置如下
- <!-- 配置sessionFactory -->
- <bean id= "sessionFactory"
- class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
- <property name= "configLocation" >
- <value>classpath:hibernate.cfg.xml</value>
- </property>
- </bean>
- <!-- 配置事务管理器 -->
- <bean id= "transactionManager"
- class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
- <property name= "sessionFactory" >
- <ref bean= "sessionFactory" />
- </property>
- </bean>
-
- <!-- 配置事务的传播特性 -->
- <tx:advice id= "txAdvice" transaction-manager= "transactionManager" >
- <tx:attributes>
- <tx:method name= "*" propagation= "REQUIRED" />
- </tx:attributes>
- </tx:advice>
-
-
- <bean id= "userDao" class = "com.dengmin.pager .dao.UserDaoImpl" >
- <property name= "sessionFactory" ref= "sessionFactory" />
- </bean>
-
- <bean name= "/userManager"
- class = "com.dengmin.pager .actions.UserAction" scope= "prototype" >
- <property name= "userDao" ref= "userDao" />
- </bean>
- <!-- 配置sessionFactory -->
- <bean id= "sessionFactory"
- class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
- <property name= "configLocation" >
- <value>classpath:hibernate.cfg.xml</value>
- </property>
- </bean>
- <!-- 配置事务管理器 -->
- <bean id= "transactionManager"
- class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
- <property name= "sessionFactory" >
- <ref bean= "sessionFactory" />
- </property>
- </bean>
-
- <!-- 配置事务的传播特性 -->
- <tx:advice id= "txAdvice" transaction-manager= "transactionManager" >
- <tx:attributes>
- <tx:method name= "*" propagation= "REQUIRED" />
- </tx:attributes>
- </tx:advice>
-
-
- <bean id= "userDao" class = "com.dengmin.pager .dao.UserDaoImpl" >
- <property name= "sessionFactory" ref= "sessionFactory" />
- </bean>
-
- <bean name= "/userManager"
- class = "com.dengmin.pager .actions.UserAction" scope= "prototype" >
- <property name= "userDao" ref= "userDao" />
- </bean>
- <!-- 配 置sessionFactory -->
- <bean id="sessionFactory"
- class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
- <property name="configLocation" >
- <value>classpath:hibernate.cfg.xml</value>
- </property>
- </bean>
- <!-- 配 置事务管理器 -->
- <bean id="transactionManager"
- class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
- <property name="sessionFactory" >
- <ref bean="sessionFactory" />
- </property>
- </bean>
-
- <!-- 配 置事务的传播特性 -->
- <tx:advice id="txAdvice" transaction-manager= "transactionManager" >
- <tx:attributes>
- <tx:method name="*" propagation= "REQUIRED" />
- </tx:attributes>
- </tx:advice>
-
-
- <bean id="userDao" class = "com.dengmin.pager .dao.UserDaoImpl" >
- <property name="sessionFactory" ref= "sessionFactory" />
- </bean>
-
- <bean name="/userManager"
- class = "com.dengmin.pager .actions.UserAction" scope= "prototype" >
- <property name="userDao" ref= "userDao" />
- </bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<bean id="userDao" class="com.dengmin.pager
.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean name="/userManager"
class="com.dengmin.pager
.actions.UserAction" scope="prototype">
<property name="userDao" ref="userDao" />
</bean>
最后是jsp页面做数据显示
这里采用了jstl和pager -taglib 的 标签库
首先将其引入:
- <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %>
- <%@ taglib prefix= "pg" uri= "http://jsptags.com/tags/navigation/pager " %>
- <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %>
- <%@ taglib prefix= "pg" uri= "http://jsptags.com/tags/navigation/pager " %>
- <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %>
- <%@ taglib prefix="pg" uri= "http://jsptags.com/tags/navigation/pager " %>
<%@ taglib
prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib
prefix="pg" uri="http://jsptags.com/tags/navigation/pager
" %>
- < body >
- < h1 > User Manager </ h1 >
- < hr />
-
- < table width = "778" border = "0" cellPadding = "0" cellSpacing = "1" bgcolor = "#6386d6" >
- < tr bgcolor = "#EFF3F7" >
- < TD align = "center" > ID </ TD >
- < TD align = "center" > 名 称 </ TD >
- < TD
分享到:
评论