Spring+Hibernate+Struts实现分页
Spring集成Hibernate,使得SessionFactory的管理更加方便,可以方便地获取到一个HibernateTempalte的模板实例,对数据库的操作非常方便。
Spring集成Struts,先把Struts的Action用Spring的ActionSupport抽象出来,比如定义一个名称为SpringAction的的ActionSupport的子类,其中SpringAction中启动IoC容器,通过ApplicationContext获取业务bean。这需要使用一个Struts插件来初始化IoC容器,从而获取到一个ApplicationContext。可以通过让每个具体的Action(每个具体的Action意思是实现Struts的Action的子类)都继承自上面定义的SpringAction,实现对模型的调用,以及派发视图。
下面通过实现分页来说明。
实现思路
Spring集成Hibernate,直接使用Hibernatede配置文件hibernate.cfg.xml,注入一个SessionFactory,从而在DAO中获取到一个HibernateTemplate。
(注意,下面工程结构中,实际上包org.shirdrn.spring.dao及其包org.shirdrn.spring.daoImpl中的接口和类并没有使用到。)
org.shirdrn.spring.util.Page类是一个页面的实体类,没有加入任何分页逻辑,而实现分页逻辑的抽象是在org.shirdrn.spring.util.PageUtil类中,该类是对所有需要实现分页的业务对象分页逻辑的抽象,而且PageUtil类继承自HibernateDaoSupport类,它是一个具有特殊业务需(分页)要的DAO,只是为获取分页结果而定义的,只要注入一个SessionFactory就可以方便地检索数据库了。所有的具体的业务对象如果需要分页显示的数据库记录,都可以从继承PageUtil类。
包org.shirdrn.spring.pages中有一个MyUserPage类,该类继承自PageUtil类,是一个具体实现分页逻辑的类,只对MyUser实体(对应数据库中myUser表)的分页进行处理。
开发环境
Windows 2003 SP2 + SQL Server 2000 + Eclipse 3.2 + JDK 1.5 + MyEclipse 5.0 + Tomcat 5.5.9 + Spring 2.0 + Hibernate 3.0 + Struts 1.2
工程结构
SpringHibernateStrutsPage
│ .classpath
│ .myhibernatedata
│ .mymetadata
│ .mystrutsdata
│ .project
│ .springBeans
│
├─.myeclipse
├─src
│ │ hibernate.cfg.xml
│ │
│ └─org
│ └─shirdrn
│ ├─spring
│ │ ├─action
│ │ │ SpringAction.java
│ │ │
│ │ ├─dao
│ │ │ │ MyUserDAO.java
│ │ │ │
│ │ │ └─impl
│ │ │ MyUserDAOImpl.java
│ │ │
│ │ ├─entity
│ │ │ MyUser.hbm.xml
│ │ │ MyUser.java
│ │ │
│ │ ├─pages
│ │ │ MyUserPage.java
│ │ │
│ │ └─util
│ │ Page.java
│ │ PageUtil.java
│ │
│ └─struts
│ │ ApplicationResources.properties
│ │
│ └─action
│ ListMyUsersAction.java
│
└─WebRoot
│ listmyusers.jsp
│
├─META-INF
│ MANIFEST.MF
│
└─WEB-INF
│ .struts-config.mex
│ action-servlet.xml
│ struts-bean.tld
│ struts-config.xml
│ struts-html.tld
│ struts-logic.tld
│ validator-rules.xml
│ web.xml
│
├─classes
│ │ hibernate.cfg.xml
│ │
│ └─org
│ └─shirdrn
│ ├─spring
│ │ ├─action
│ │ │ SpringAction.class
│ │ │
│ │ ├─dao
│ │ │ │ MyUserDAO.class
│ │ │ │
│ │ │ └─impl
│ │ │ MyUserDAOImpl.class
│ │ │
│ │ ├─entity
│ │ │ MyUser.class
│ │ │ MyUser.hbm.xml
│ │ │
│ │ ├─pages
│ │ │ MyUserPage$1.class
│ │ │ MyUserPage.class
│ │ │
│ │ └─util
│ │ Page.class
│ │ PageUtil.class
│ │
│ └─struts
│ │ ApplicationResources.properties
│ │
│ └─action
│ ListMyUsersAction.class
│
└─lib
开发过程
(1) Hibernate准备
编写Hibernate配置文件hibernate.cfg.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://localhost:1433;databasename=shirdrn
</property>
<property name="connection.username">sa</property>
<property name="connection.password">111111</property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="myeclipse.connection.profile">MSSQL</property>
<property name="show_sql">true</property>
<mapping resource="org/shirdrn/spring/entity/MyUser.hbm.xml" />
</session-factory>
</hibernate-configuration>
实现Hibernate映射文件MyUser.hbm.xml,及其对应的POJO:
这个非常容易,就省略了。
(2) Spring集成Hibernate
在/WEB-INF/目录下面,新建一个action-servlet.xml的Spring配置文件,因为后面要和Struts集成,所以根据格式<SERVLETNAME>-servlet.xml,应该使用Struts的Servlet映射名称。
Spring集成Hibernate,在action-servlet.xml中配置如下:
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
(3) 页面实体及分页逻辑实现
Page是一个页面实体类,如下:
package org.shirdrn.spring.util;
import java.util.List;
public class Page {
private int pageSize;
private int totalPage;
private int rowCount;
private int currentPage;
private int prePage;
private int nextPage;
private boolean hasNextPage; // 是否有下一页
private boolean hasPreviousPage; // 是否有前一页
private List list;
public Page(){
this.pageSize = 10;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPrePage() {
return prePage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
}
PageUtil类是一个分页逻辑的抽象,需要分页的话,只需要继承自它便可以方便地实现分页,可见,PageUtil可以重用,如下所示:
package org.shirdrn.spring.util;
import org.shirdrn.spring.pages.MyUserPage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class PageUtil extends HibernateDaoSupport {
public Page page;
public int start;
public PageUtil(){
}
public void setPreOrNextBoolean(){
if(page.getCurrentPage()<=1){
page.setHasPreviousPage(false);
}
else{
page.setHasPreviousPage(true);
}
if(page.getCurrentPage()>=page.getTotalPage()){
page.setHasNextPage(false);
}
else{
page.setHasNextPage(true);
}
}
public void setCurrentPage(){
if(start<1){
page.setCurrentPage(1);
}
if(start>page.getTotalPage()){
page.setCurrentPage(page.getTotalPage());
}
page.setCurrentPage(start);
}
public void setPrePage(){
page.setPrePage(page.getCurrentPage()-1);
}
public void setNextPage(){
page.setNextPage(page.getCurrentPage()+1);
}
public void setTotalPage(){
int rowCount = getRowCount();
int pageSize = page.getPageSize();
if(rowCount>pageSize){
if(rowCount%pageSize == 0){
page.setTotalPage(rowCount/pageSize);
}
else{
page.setTotalPage(1+(rowCount/pageSize));
}
}
else{
page.setTotalPage(1);
}
}
public void setRowCount(){
page.setRowCount(getRowCount());
}
public int getRowCount(){
return 0;
}
public int getStartIndex(){
int startIndex = 0;
if(start<0){
startIndex = 0;
}
else{
if(start>page.getTotalPage()){
startIndex = page.getPageSize()*(page.getTotalPage()-1);
}
else{
startIndex = page.getPageSize()*(start-1);
}
}
return startIndex;
}
public Page getPage(){
return page;
}
}
(4) 具体的业务对象分页实现
这里,MyUser类是一个POJO,查询出结果需要分页显示。
MyUser的分页实现类为MyUserPage类,它应该继承PageUtil类。因为PageUtil类继承了HibernateDaoSupport类,所以需要注入一个SessionFactory,在action-servlet.xml中配置如下所示:
<bean id="myUserPage" abstract="false"
class="org.shirdrn.spring.pages.MyUserPage"
lazy-init="default" autowire="default" dependency-check="default">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
</beans>
具体地,MyUserPage类实现如下所示:
package org.shirdrn.spring.pages;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.shirdrn.spring.util.Page;
import org.shirdrn.spring.util.PageUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
public class MyUserPage extends PageUtil {
private String hql;
public void init(int start,String hql){ // 通过init方法实现一个页面的初始化
page = new Page();
this.hql = hql;
this.start = start;
setRowCount();
setTotalPage();
setCurrentPage();
setPrePage();
setNextPage();
setPreOrNextBoolean();
}
@Override
public int getRowCount(){
List list = getHibernateTemplate().find(hql);
if(list.isEmpty()){
return 0;
}
return list.size();
}
@Override
public Page getPage(){
List list = (List)getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(getStartIndex());
query.setMaxResults(page.getPageSize());
return query.list();
}
});
page.setList(list);
return page;
}
}
上面实现,使用了HQL检索方式实现查询数据库记录。
在MyUserPage中可以使用带参数的构造方法实现页面的初始化,但是在使用构造方法注入的时候,很容易出错,而且不直观,所以直接通过调用实现的一个init方法来初始化。
(6) Spring集成Struts
需要在Struts的配置文件struts-config.xml中配置一个插件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings >
<action path="/listMyUsers" type="org.shirdrn.struts.action.ListMyUsersAction">
<forward
name="listmyusers"
path="/listmyusers.jsp"
redirect="true" />
</action>
</action-mappings>
<message-resources parameter="org.shirdrn.struts.ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/action-servlet.xml"/>
</plug-in>
</struts-config>
只要这样配置好,Struts才能初始化Spring的IoC容器,否则Action无法工作。
(7) Action的实现
SpringAction继承了Spring的ActionSupport,我们将用它来替代所有继承自Struts的Action类的Action。SpringAction的实现如下所示:
package org.shirdrn.spring.action;
import org.shirdrn.spring.pages.MyUserPage;
import org.springframework.web.struts.ActionSupport;
public class SpringAction extends ActionSupport {
public MyUserPage getMyUserPage(){
return (MyUserPage)getWebApplicationContext().getBean("myUserPage");
}
}
这里,SpringAction就实现了获取一个MyUserPage的实例的功能,从而通过这个实例实现分页。
然后,实现一个ListMyUsersAction类,它继承自SpringAction,能够启动Spring的IoC容器,并且获取一个MyUserPage的实例。ListMyUsersAction类的实现如下所示:
package org.shirdrn.struts.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.shirdrn.spring.action.SpringAction;
import org.shirdrn.spring.pages.MyUserPage;
import org.shirdrn.spring.util.Page;
public class ListMyUsersAction extends SpringAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
int pno = (new Integer(request.getParameter("pno"))).intValue();
String hql = "from MyUser";
MyUserPage myUserPage = getMyUserPage();
myUserPage.init(pno, hql);
Page myUserOnePage = myUserPage.getPage();
List myUserList = myUserPage.getPage().getList();
HttpSession session = request.getSession();
session.setAttribute("myUserOnePage", myUserOnePage);
session.setAttribute("myUserList", myUserList);
return mapping.findForward("listmyusers");
}
}
(8) 视图的实现
JSP页面,使用Struts标签实现分页页面的显示。
JSP页面listmyusers.jsp如下所示:
<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html:html locale="true">
<head>
<base href="<%=basePath%>">
<title>查询记录列表</title>
<style type="text/css">
body{
background:#000000;
font-size:12px;
color:yellow;
}
a:link{
color:yellow;
text-decoration:none;
}
a:hover{
color:red;
text-decoration:none;
}
a:visited{
color:yellow;
text-decoration:none;
}
</style>
</head>
<body>
<table width="60%" align="center" bgcolor="green" border="1">
<tr>
<th colspan="10">MyUser查询列表</th>
</tr>
<tr align="center">
<td width="20%"><b>ID</b></td>
<td width="20%"><b>姓名</b></td>
<td width="20%"><b>性别</b></td>
<td width="20%"><b>年龄</b></td>
<td width="20%"><b>住址</b></td>
</tr>
<logic:present name="myUserList">
<logic:iterate id="myUser" name="myUserList" type="org.shirdrn.spring.entity.MyUser">
<logic:present name="myUser">
<tr align="center">
<td width="20%" height="10"><bean:write name="myUser" property="id"/></td>
<td width="20%" height="10"><bean:write name="myUser" property="userName"/></td>
<td width="20%" height="10"><bean:write name="myUser" property="gender"/></td>
<td width="20%" height="10"><bean:write name="myUser" property="age"/></td>
<td width="20%" height="10"><bean:write name="myUser" property="addr"/></td>
</tr>
</logic:present>
</logic:iterate>
</logic:present>
</table>
<table align="center" width="60%" bgcolor="green" border="1">
<tr>
<td align="center" colspan="10">
<logic:present name="myUserOnePage">
<html:link page="/listMyUsers.do?pno=1">首页</html:link>
<logic:equal name="myUserOnePage" property="hasPreviousPage" value="false">上一页</logic:equal>
<logic:equal name="myUserOnePage" property="hasPreviousPage" value="true">
<a href="<%=path%>/listMyUsers.do?pno=<bean:write name="myUserOnePage" property="prePage"/>">上一页</a>
</logic:equal>
每页<bean:write name="myUserOnePage" property="pageSize"/>条记录
共<bean:write name="myUserOnePage" property="rowCount"/>条记录
当前第(<bean:write name="myUserOnePage" property="currentPage"/>/<bean:write name="myUserOnePage" property="totalPage"/>)页
<logic:equal name="myUserOnePage" property="hasNextPage" value="false">下一页</logic:equal>
<logic:equal name="myUserOnePage" property="hasNextPage" value="true">
<a href="<%=path%>/listMyUsers.do?pno=<bean:write name="myUserOnePage" property="nextPage"/>">下一页</a>
</logic:equal>
<a href="<%=path%>/listMyUsers.do?pno=<bean:write name='myUserOnePage' property='totalPage'/>">末页</a>
</logic:present>
</td>
</tr>
</table>
</body>
</html:html>
(9) 部署Web应用
配置web.xml文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
测试过程
保证将所需要的jar包都加入到CALSSPATH中以后,就可以测试了。
启动数据库服务器,保证数据库shirdrn中的表myUser中存在数据。
启动Tomcat Web Server。在浏览器地址栏中键入地址
http://localhost:8080/SpringHibernateStrutsPage/listMyUsers.do?pno=1
就可以看到数据的分页显示,效果如图所示:
OK,就这么容易。