话不多说,直接看代码,基本都有注释,很简单的例子
mysql 脚本:
并插入一定数量的数据
CREATE
TABLE
`users` (
`id` int ( 11 ) NOT NULL default ' 0 ' ,
`name` varchar ( 20 ) default NULL ,
`age` varchar ( 20 ) default NULL ,
PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = gb2312;
`id` int ( 11 ) NOT NULL default ' 0 ' ,
`name` varchar ( 20 ) default NULL ,
`age` varchar ( 20 ) default NULL ,
PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = gb2312;
BaseAction:
package
action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.ActionSupport;
public abstract class BaseAction extends ActionSupport ... {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) ...{
return doBusiness(mapping,form,request,response);
}
//具体actoin的业务方法
public abstract ActionForward doBusiness (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response);
}
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.ActionSupport;
public abstract class BaseAction extends ActionSupport ... {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) ...{
return doBusiness(mapping,form,request,response);
}
//具体actoin的业务方法
public abstract ActionForward doBusiness (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response);
}
UserListAction
/**/
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import service.IUserService;
import util.PageBean;
/** */ /**
* MyEclipse Struts
* Creation date: 09-06-2007
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class UserListAction extends BaseAction ... {
public ActionForward doBusiness(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) ...{
/**//*
* 设置一个开始索引号,如果传入的page即为下一页第一个元素的索引号,hibernate设置此startIndex为firstResut
*/
int startIndex;
if(request.getParameter("page")==null)...{
startIndex=0;
}
else...{
startIndex=Integer.parseInt(request.getParameter("page"));
}
//访问业务方法,得到封装好页数,结果等信息的PageBean
PageBean pageBean=((IUserService)this.getWebApplicationContext().getBean("userService")).getAllUsers(startIndex);
request.setAttribute("pageBean", pageBean);
return mapping.findForward("success");
}
}
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import service.IUserService;
import util.PageBean;
/** */ /**
* MyEclipse Struts
* Creation date: 09-06-2007
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class UserListAction extends BaseAction ... {
public ActionForward doBusiness(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) ...{
/**//*
* 设置一个开始索引号,如果传入的page即为下一页第一个元素的索引号,hibernate设置此startIndex为firstResut
*/
int startIndex;
if(request.getParameter("page")==null)...{
startIndex=0;
}
else...{
startIndex=Integer.parseInt(request.getParameter("page"));
}
//访问业务方法,得到封装好页数,结果等信息的PageBean
PageBean pageBean=((IUserService)this.getWebApplicationContext().getBean("userService")).getAllUsers(startIndex);
request.setAttribute("pageBean", pageBean);
return mapping.findForward("success");
}
}
IUserService
package
service;
import util.PageBean;
public interface IUserService ... {
public PageBean getAllUsers(int firstResut);
public int getAllUsersCount();
}
import util.PageBean;
public interface IUserService ... {
public PageBean getAllUsers(int firstResut);
public int getAllUsersCount();
}
UserServiceImpl
package
service;
import util.PageBean;
import dao.IUserDAO;
public class UserServiceImpl implements IUserService ... {
private IUserDAO userDAO;
public IUserDAO getUserDAO() ...{
return userDAO;
}
public void setUserDAO(IUserDAO userDAO) ...{
this.userDAO = userDAO;
}
//获得分页后的结果
public PageBean getAllUsers(int firstResut) ...{
int totalCount=this.getAllUsersCount();
return userDAO.getAllUsers(firstResut, PageBean.getPAGE_MAX_NUMBER(),totalCount);
}
//取得记录总数
public int getAllUsersCount()...{
return userDAO.getAllUsersCount();
}
}
import util.PageBean;
import dao.IUserDAO;
public class UserServiceImpl implements IUserService ... {
private IUserDAO userDAO;
public IUserDAO getUserDAO() ...{
return userDAO;
}
public void setUserDAO(IUserDAO userDAO) ...{
this.userDAO = userDAO;
}
//获得分页后的结果
public PageBean getAllUsers(int firstResut) ...{
int totalCount=this.getAllUsersCount();
return userDAO.getAllUsers(firstResut, PageBean.getPAGE_MAX_NUMBER(),totalCount);
}
//取得记录总数
public int getAllUsersCount()...{
return userDAO.getAllUsersCount();
}
}
IUserDAO
package
dao;
import util.PageBean;
import org.hibernate.dialect.MySQL5Dialect;
public interface IUserDAO ... {
public PageBean getAllUsers(int firstResut,int maxResult,int totalCount);
public int getAllUsersCount();
}
import util.PageBean;
import org.hibernate.dialect.MySQL5Dialect;
public interface IUserDAO ... {
public PageBean getAllUsers(int firstResut,int maxResult,int totalCount);
public int getAllUsersCount();
}
UserDAOImpl
package
dao;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import util.PageBean;
public class UserDAOImpl extends HibernateDaoSupport implements IUserDAO ... {
//根据传入的开始索引号和PageBean中定义的每页最大数,查询结果,封装成PageBean
public PageBean getAllUsers(final int firstResult, final int maxResult,final int totalCount) ...{
return (PageBean)getHibernateTemplate().execute(new HibernateCallback()...{
public Object doInHibernate(Session session) throws HibernateException, SQLException...{
Query query=session.createQuery("from Users");
query.setFirstResult(firstResult-1);
query.setMaxResults(maxResult);
List result=query.list();
PageBean pageBean=new PageBean(result,totalCount,firstResult);
return pageBean;
}
}, true);
}
//获得记录总数
public int getAllUsersCount() ...{
String hql = "select count(*) from Users as user";
Long count = (Long)getHibernateTemplate().find(hql).listIterator().next();
return count.intValue();
}
}
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import util.PageBean;
public class UserDAOImpl extends HibernateDaoSupport implements IUserDAO ... {
//根据传入的开始索引号和PageBean中定义的每页最大数,查询结果,封装成PageBean
public PageBean getAllUsers(final int firstResult, final int maxResult,final int totalCount) ...{
return (PageBean)getHibernateTemplate().execute(new HibernateCallback()...{
public Object doInHibernate(Session session) throws HibernateException, SQLException...{
Query query=session.createQuery("from Users");
query.setFirstResult(firstResult-1);
query.setMaxResults(maxResult);
List result=query.list();
PageBean pageBean=new PageBean(result,totalCount,firstResult);
return pageBean;
}
}, true);
}
//获得记录总数
public int getAllUsersCount() ...{
String hql = "select count(*) from Users as user";
Long count = (Long)getHibernateTemplate().find(hql).listIterator().next();
return count.intValue();
}
}
domain:
package
domain;
/** */ /**
* Users generated by MyEclipse Persistence Tools
*/
public class Users implements java.io.Serializable ... {
// Fields
private Integer id;
private String name;
private String age;
// Constructors
/** *//** default constructor */
public Users() ...{
}
/** *//** minimal constructor */
public Users(Integer id) ...{
this.id = id;
}
/** *//** full constructor */
public Users(Integer id, String name, String age) ...{
this.id = id;
this.name = name;
this.age = age;
}
// Property accessors
public Integer getId() ...{
return this.id;
}
public void setId(Integer id) ...{
this.id = id;
}
public String getName() ...{
return this.name;
}
public void setName(String name) ...{
this.name = name;
}
public String getAge() ...{
return this.age;
}
public void setAge(String age) ...{
this.age = age;
}
}
/** */ /**
* Users generated by MyEclipse Persistence Tools
*/
public class Users implements java.io.Serializable ... {
// Fields
private Integer id;
private String name;
private String age;
// Constructors
/** *//** default constructor */
public Users() ...{
}
/** *//** minimal constructor */
public Users(Integer id) ...{
this.id = id;
}
/** *//** full constructor */
public Users(Integer id, String name, String age) ...{
this.id = id;
this.name = name;
this.age = age;
}
// Property accessors
public Integer getId() ...{
return this.id;
}
public void setId(Integer id) ...{
this.id = id;
}
public String getName() ...{
return this.name;
}
public void setName(String name) ...{
this.name = name;
}
public String getAge() ...{
return this.age;
}
public void setAge(String age) ...{
this.age = age;
}
}
user.hbm.xml
<?
xml version="1.0" encoding="utf-8"
?>
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
< hibernate-mapping >
< class name ="domain.Users" table ="users" catalog ="page" >
< id name ="id" type ="java.lang.Integer" >
< column name ="id" />
< generator class ="assigned" />
</ id >
< property name ="name" type ="java.lang.String" >
< column name ="name" length ="20" />
</ property >
< property name ="age" type ="java.lang.String" >
< column name ="age" length ="20" />
</ property >
</ class >
</ hibernate-mapping >
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
< hibernate-mapping >
< class name ="domain.Users" table ="users" catalog ="page" >
< id name ="id" type ="java.lang.Integer" >
< column name ="id" />
< generator class ="assigned" />
</ id >
< property name ="name" type ="java.lang.String" >
< column name ="name" length ="20" />
</ property >
< property name ="age" type ="java.lang.String" >
< column name ="age" length ="20" />
</ property >
</ class >
</ hibernate-mapping >
ContextLoaderListener
package
util;
import javax.servlet.ServletContextEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class ContextLoaderListener extends
org.springframework.web.context.ContextLoaderListener ... {
public void contextInitialized(ServletContextEvent event) ...{
// TODO Auto-generated method stub
super.contextInitialized(event);
ApplicationContext context=WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
}
}
import javax.servlet.ServletContextEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class ContextLoaderListener extends
org.springframework.web.context.ContextLoaderListener ... {
public void contextInitialized(ServletContextEvent event) ...{
// TODO Auto-generated method stub
super.contextInitialized(event);
ApplicationContext context=WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
}
}
PageBean
package
util;
import java.util.List;
public class PageBean ... {
private static final int PAGE_MAX_NUMBER=5;//每页显示的最大数
private int currentPage=0; //当前页码
private int nextPage=0; //下一页码
private int previousPage=0; //上一页码
private List resultList=null; //数据集合
private int nextReusltIndex=1+PAGE_MAX_NUMBER; //下一页开始的数据索引号
private int previousResultIndex=0; //上一页开始的数据索引号
private int currentResultIndex=0; //当前页开始的索引号
private int pageCount=0; //总页数
private int totlaCount=0; //总记录数
private int firstPageResultIndex=0; //第一页数据索引
private int lastPAgeResultIndex=0; //最后一页数据索引
public int getFirstPageResultIndex() ...{
return 0;
}
public int getLastPAgeResultIndex() ...{
if(resultList.size()%PAGE_MAX_NUMBER==0)...{
return (this.getPageCount()-1)*PAGE_MAX_NUMBER+1;
}else...{
return (resultList.size()-resultList.size()%PAGE_MAX_NUMBER)+1;
}
}
public PageBean()...{
}
public PageBean(List resultList, int totlaCount,int currentResultIndex) ...{
super();
this.resultList = resultList;
this.currentResultIndex = currentResultIndex;
this.totlaCount = totlaCount;
}
public int getCurrentPage() ...{
return this.getCurrentResultIndex()/this.getPAGE_MAX_NUMBER()+1;
}
public int getNextPage() ...{
if(this.getCurrentPage()==this.getPageCount())...{
return this.getCurrentResultIndex();
}
else...{
return this.getCurrentPage()+1;
}
}
public int getPreviousPage() ...{
if(this.getCurrentPage()==1)...{
return this.getCurrentPage();
}
else...{
return this.getCurrentPage()-1;
}
}
public List getResultList() ...{
return resultList;
}
public void setResultList(List resultList) ...{
this.resultList = resultList;
}
public int getNextReusltIndex() ...{
if(this.getCurrentPage()==this.getPageCount())...{
return this.getCurrentResultIndex();
}
else...{
return this.getCurrentResultIndex()+this.getPAGE_MAX_NUMBER()+1;
}
}
public int getPreviousResultIndex() ...{
if(this.getCurrentPage()==1)...{
return this.getCurrentResultIndex();
}
else...{
return this.getCurrentResultIndex()-this.getPAGE_MAX_NUMBER();
}
}
public int getCurrentResultIndex() ...{
return currentResultIndex;
}
public void setCurrentResultIndex(int currentResultIndex) ...{
this.currentResultIndex = currentResultIndex;
}
public int getPageCount() ...{
if(this.getTotlaCount()==0)...{
return 0;
}
else...{
return this.getTotlaCount()/PAGE_MAX_NUMBER+1;
}
}
public static int getPAGE_MAX_NUMBER() ...{
return PAGE_MAX_NUMBER;
}
public int getTotlaCount() ...{
return totlaCount;
}
}
import java.util.List;
public class PageBean ... {
private static final int PAGE_MAX_NUMBER=5;//每页显示的最大数
private int currentPage=0; //当前页码
private int nextPage=0; //下一页码
private int previousPage=0; //上一页码
private List resultList=null; //数据集合
private int nextReusltIndex=1+PAGE_MAX_NUMBER; //下一页开始的数据索引号
private int previousResultIndex=0; //上一页开始的数据索引号
private int currentResultIndex=0; //当前页开始的索引号
private int pageCount=0; //总页数
private int totlaCount=0; //总记录数
private int firstPageResultIndex=0; //第一页数据索引
private int lastPAgeResultIndex=0; //最后一页数据索引
public int getFirstPageResultIndex() ...{
return 0;
}
public int getLastPAgeResultIndex() ...{
if(resultList.size()%PAGE_MAX_NUMBER==0)...{
return (this.getPageCount()-1)*PAGE_MAX_NUMBER+1;
}else...{
return (resultList.size()-resultList.size()%PAGE_MAX_NUMBER)+1;
}
}
public PageBean()...{
}
public PageBean(List resultList, int totlaCount,int currentResultIndex) ...{
super();
this.resultList = resultList;
this.currentResultIndex = currentResultIndex;
this.totlaCount = totlaCount;
}
public int getCurrentPage() ...{
return this.getCurrentResultIndex()/this.getPAGE_MAX_NUMBER()+1;
}
public int getNextPage() ...{
if(this.getCurrentPage()==this.getPageCount())...{
return this.getCurrentResultIndex();
}
else...{
return this.getCurrentPage()+1;
}
}
public int getPreviousPage() ...{
if(this.getCurrentPage()==1)...{
return this.getCurrentPage();
}
else...{
return this.getCurrentPage()-1;
}
}
public List getResultList() ...{
return resultList;
}
public void setResultList(List resultList) ...{
this.resultList = resultList;
}
public int getNextReusltIndex() ...{
if(this.getCurrentPage()==this.getPageCount())...{
return this.getCurrentResultIndex();
}
else...{
return this.getCurrentResultIndex()+this.getPAGE_MAX_NUMBER()+1;
}
}
public int getPreviousResultIndex() ...{
if(this.getCurrentPage()==1)...{
return this.getCurrentResultIndex();
}
else...{
return this.getCurrentResultIndex()-this.getPAGE_MAX_NUMBER();
}
}
public int getCurrentResultIndex() ...{
return currentResultIndex;
}
public void setCurrentResultIndex(int currentResultIndex) ...{
this.currentResultIndex = currentResultIndex;
}
public int getPageCount() ...{
if(this.getTotlaCount()==0)...{
return 0;
}
else...{
return this.getTotlaCount()/PAGE_MAX_NUMBER+1;
}
}
public static int getPAGE_MAX_NUMBER() ...{
return PAGE_MAX_NUMBER;
}
public int getTotlaCount() ...{
return totlaCount;
}
}
PageTag
package
util;
import java.io.IOException;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class PageTag extends SimpleTagSupport ... {
private String pagebean;
public void doTag() throws JspException, IOException ...{
super.doTag();
PageBean pageBean=(PageBean)getJspContext().getAttribute(this.getPagebean());
int currentPage=pageBean.getCurrentPage();
int nextPage=pageBean.getNextPage();
int previousPage=pageBean.getPreviousPage();
int nextResultIndex=pageBean.getNextReusltIndex();
int previousResultIndex=pageBean.getPreviousResultIndex();
int pageCount=pageBean.getPageCount();
int firstResultIndex=pageBean.getFirstPageResultIndex();
int lastResultIndex=pageBean.getLastPAgeResultIndex();
JspWriter out=getJspContext().getOut();
StringBuffer buffer=new StringBuffer();
buffer.append("<a href="/PageSwitch/userList.do?page="+firstResultIndex+"">"+"第一页</a>");
buffer.append("<a href="/PageSwitch/userList.do?page="+previousResultIndex+"">"+"上一页</a>");
buffer.append("<a href="/PageSwitch/userList.do?page="+nextResultIndex+"">"+"下一页</a>");
buffer.append("<a href="/PageSwitch/userList.do?page="+lastResultIndex+"">"+"末一页</a>");
buffer.append("第"+currentPage+"页,共"+pageCount+"页");
out.print(buffer.toString());
}
public String getPagebean() ...{
return pagebean;
}
public void setPagebean(String pagebean) ...{
this.pagebean = pagebean;
}
}
import java.io.IOException;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class PageTag extends SimpleTagSupport ... {
private String pagebean;
public void doTag() throws JspException, IOException ...{
super.doTag();
PageBean pageBean=(PageBean)getJspContext().getAttribute(this.getPagebean());
int currentPage=pageBean.getCurrentPage();
int nextPage=pageBean.getNextPage();
int previousPage=pageBean.getPreviousPage();
int nextResultIndex=pageBean.getNextReusltIndex();
int previousResultIndex=pageBean.getPreviousResultIndex();
int pageCount=pageBean.getPageCount();
int firstResultIndex=pageBean.getFirstPageResultIndex();
int lastResultIndex=pageBean.getLastPAgeResultIndex();
JspWriter out=getJspContext().getOut();
StringBuffer buffer=new StringBuffer();
buffer.append("<a href="/PageSwitch/userList.do?page="+firstResultIndex+"">"+"第一页</a>");
buffer.append("<a href="/PageSwitch/userList.do?page="+previousResultIndex+"">"+"上一页</a>");
buffer.append("<a href="/PageSwitch/userList.do?page="+nextResultIndex+"">"+"下一页</a>");
buffer.append("<a href="/PageSwitch/userList.do?page="+lastResultIndex+"">"+"末一页</a>");
buffer.append("第"+currentPage+"页,共"+pageCount+"页");
out.print(buffer.toString());
}
public String getPagebean() ...{
return pagebean;
}
public void setPagebean(String pagebean) ...{
this.pagebean = pagebean;
}
}
page.tld
<?
xml version="1.0" encoding="UTF-8"
?>
< taglib xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
version ="2.0" >
< tlib-version > 1.0 </ tlib-version >
< jsp-version > 2.0 </ jsp-version >
< tag >
< name > page </ name >
< tagclass > util.PageTag </ tagclass >
< bodycontent > empty </ bodycontent >
< attribute >
< name > pagebean </ name >
< required > true </ required >
< rtexprvalue > false </ rtexprvalue >
</ attribute >
</ tag >
</ taglib >
< taglib xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
version ="2.0" >
< tlib-version > 1.0 </ tlib-version >
< jsp-version > 2.0 </ jsp-version >
< tag >
< name > page </ name >
< tagclass > util.PageTag </ tagclass >
< bodycontent > empty </ bodycontent >
< attribute >
< name > pagebean </ name >
< required > true </ required >
< rtexprvalue > false </ rtexprvalue >
</ attribute >
</ tag >
</ taglib >
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" >
< context-param >
< param-name > contextConfigLocation </ param-name >
< param-value > /WEB-INF/applicationContext.xml </ param-value >
</ context-param >
< listener >
< listener-class > util.ContextLoaderListener </ listener-class >
</ listener >
< 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 > index.jsp </ welcome-file >
</ welcome-file-list >
< jsp-config >
< taglib >
< taglib-uri > pageTag </ taglib-uri >
< taglib-location > /WEB-INF/page.tld </ taglib-location >
</ taglib >
</ jsp-config >
</ web-app >
< 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" >
< context-param >
< param-name > contextConfigLocation </ param-name >
< param-value > /WEB-INF/applicationContext.xml </ param-value >
</ context-param >
< listener >
< listener-class > util.ContextLoaderListener </ listener-class >
</ listener >
< 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 > index.jsp </ welcome-file >
</ welcome-file-list >
< jsp-config >
< taglib >
< taglib-uri > pageTag </ taglib-uri >
< taglib-location > /WEB-INF/page.tld </ taglib-location >
</ taglib >
</ jsp-config >
</ web-app >
applicationContext.xml
<?
xml version="1.0" encoding="UTF-8"
?>
< beans
xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" >
< property name ="driverClassName" >
< value > com.mysql.jdbc.Driver </ value >
</ property >
< property name ="url" >
< value > jdbc:mysql://localhost:3306/page </ value >
</ property >
< property name ="username" >
< value > root </ value >
</ property >
< property name ="password" >
< value > 1234 </ value >
</ property >
</ bean >
< bean id ="sessionFactory" class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name ="dataSource" >
< ref bean ="dataSource" />
</ property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" > org.hibernate.dialect.MySQL5Dialect </ prop >
< prop key ="hibernate.show_sql" > false </ prop >
</ props >
</ property >
< property name ="mappingResources" >
< list >
< value > domain/Users.hbm.xml </ value >
</ list >
</ property >
</ bean >
< bean id ="transactionManager" class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property name ="sessionFactory" >
< ref bean ="sessionFactory" />
</ property >
</ bean >
< bean id ="userDAO" class ="dao.UserDAOImpl" >
< property name ="sessionFactory" >
< ref bean ="sessionFactory" />
</ property >
</ bean >
< bean id ="userService" class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
< property name ="transactionManager" >
< ref bean ="transactionManager" />
</ property >
< property name ="target" >
< bean class ="service.UserServiceImpl" >
< property name ="userDAO" >
< ref bean ="userDAO" />
</ property >
</ bean >
</ property >
< property name ="transactionAttributes" >
< props >
< prop key ="get*" > PROPAGATION_REQUIRED </ prop >
</ props >
</ property >
</ bean >
</ beans >
< beans
xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" >
< property name ="driverClassName" >
< value > com.mysql.jdbc.Driver </ value >
</ property >
< property name ="url" >
< value > jdbc:mysql://localhost:3306/page </ value >
</ property >
< property name ="username" >
< value > root </ value >
</ property >
< property name ="password" >
< value > 1234 </ value >
</ property >
</ bean >
< bean id ="sessionFactory" class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name ="dataSource" >
< ref bean ="dataSource" />
</ property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" > org.hibernate.dialect.MySQL5Dialect </ prop >
< prop key ="hibernate.show_sql" > false </ prop >
</ props >
</ property >
< property name ="mappingResources" >
< list >
< value > domain/Users.hbm.xml </ value >
</ list >
</ property >
</ bean >
< bean id ="transactionManager" class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property name ="sessionFactory" >
< ref bean ="sessionFactory" />
</ property >
</ bean >
< bean id ="userDAO" class ="dao.UserDAOImpl" >
< property name ="sessionFactory" >
< ref bean ="sessionFactory" />
</ property >
</ bean >
< bean id ="userService" class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
< property name ="transactionManager" >
< ref bean ="transactionManager" />
</ property >
< property name ="target" >
< bean class ="service.UserServiceImpl" >
< property name ="userDAO" >
< ref bean ="userDAO" />
</ property >
</ bean >
</ property >
< property name ="transactionAttributes" >
< props >
< prop key ="get*" > PROPAGATION_REQUIRED </ prop >
</ props >
</ property >
</ bean >
</ beans >
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 ="/userList" type ="action.UserListAction" >
< forward name ="success" path ="/result.jsp" ></ forward >
</ action >
</ action-mappings >
< message-resources parameter ="ApplicationResources" />
</ struts-config >
<! 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 ="/userList" type ="action.UserListAction" >
< forward name ="success" path ="/result.jsp" ></ forward >
</ action >
</ action-mappings >
< message-resources parameter ="ApplicationResources" />
</ struts-config >
reuslt.jsp
<%
...
@page language="java" contentType="text/html;charset=gb2312"
%>
<% ... @ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% ... @ taglib uri="/WEB-INF/page.tld" prefix="page" %>
< html >
< head >
</ head >
< body >
< table border ="1" >
< tr >
< td > id </ td >
< td > name </ td >
< td > age </ td >
</ tr >
< c:forEach items ="${pageBean.resultList}" var ="item" >
< tr >
< td > < c:out value ="${item.id}" ></ c:out ></ td >
< td > < c:out value ="${item.name}" ></ c:out ></ td >
< td > < c:out value ="${item.age}" ></ c:out ></ td >
</ tr >
</ c:forEach >
</ table >
<!-- 把request中的pageBean对象保存到page范围内,供customer jsp tag访问 -->
< c:set value ="${pageBean}" var ="pageBean" scope ="page" />
< page:page pagebean ="pageBean" />
</ body >
</ html >
<% ... @ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% ... @ taglib uri="/WEB-INF/page.tld" prefix="page" %>
< html >
< head >
</ head >
< body >
< table border ="1" >
< tr >
< td > id </ td >
< td > name </ td >
< td > age </ td >
</ tr >
< c:forEach items ="${pageBean.resultList}" var ="item" >
< tr >
< td > < c:out value ="${item.id}" ></ c:out ></ td >
< td > < c:out value ="${item.name}" ></ c:out ></ td >
< td > < c:out value ="${item.age}" ></ c:out ></ td >
</ tr >
</ c:forEach >
</ table >
<!-- 把request中的pageBean对象保存到page范围内,供customer jsp tag访问 -->
< c:set value ="${pageBean}" var ="pageBean" scope ="page" />
< page:page pagebean ="pageBean" />
</ body >
</ html >
启动入口:
http://localhost:81/PageSwitch/userList.do
页面如下(我建立了23个数据,每页5个):
id | name | age |
1 | john1 | 22 |
2 | john2 | 22 |
3 | john3 | 22 |
4 | john4 | 22 |
5 | john5 | 22 |