在SSH框架下,用Hibernate+JQuery+JS制作一个分页系统

试题:开发如下界面

代码:

 

页面:UserSearch.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">    
    <title>查询用户信息</title>
    <style>
.even
{ background:#F8FBFE;} 

.odd
{ background:#EEF0F7;} 

.borderstyle
{
	border-bottom: 1px solid #FFF;
	border-left: 1px solid #FFF;
	font-size: 12px;
	line-height: 180%;
	font-family: Arial, Helvetica, sans-serif;
}

.tbfont
{ text-align:center;
  font-size:100%;
  font-weight:normal;}
  
.textcenter
{text-align:center;
 font-size: 12px;
 line-height: 180%;
 font-family: Arial, Helvetica, sans-serif;}
 
.textleft
{text-align:left;
 font-size: 12px;
 line-height: 180%;
 font-family: Arial, Helvetica, sans-serif;}
 
.textright
{text-align:right;
 font-size: 12px;
 line-height: 180%;
 font-family: Arial, Helvetica, sans-serif;}

</style>
<SCRIPT type=text/javascript src="jquery-1.8.2.js"></SCRIPT>
<script type="text/javascript">
$(function(){
	$("#search tbody > tr:odd").addClass("odd");
	$("#search tbody > tr:even").addClass("even");
});
</script>
  </head>
  
  		<body>
  		        <table id="search" cellspacing="0" cellpadding="0" width="700" align="center" border="0" class="tbfont">
        		<thead>			
                <tr bgcolor="#CAE8EA">
                      <td width="300" height="25" class="borderstyle">序号</td>
                      <td width="200" height="25" class="borderstyle">用户名</td>
                      <td width="200" height="25" class="borderstyle">密码</td>
                    </tr>    
                </thead>        
                 <tbody>
                 <s:iterator value="userList" var="user"> 
                  <tr class="borderstyle">
                    <td class="borderstyle"><s:property value="#attr.user.userId"/></td>
                    <td class="borderstyle"><s:property value="#attr.user.userName"/></td>
                    <td class="borderstyle"><s:property value="#attr.user.userPasswd"/></td>
                  </tr>
                  </s:iterator>
                  </tbody>
                </table>
                <br>                                          
              <table id="tb" width="700" border="0" cellspacing="0" cellpadding="0" align="center">
                <tr>             
                <td width="80" class="textleft">每页<s:textfield id="page" name="everyPage" cssStyle="width:25px"/> 条</td>                              
                  <td width="189" class="textright"><a href="search.action?currentPage=1" id="shouye">首页</a></td>               
                  <td width="75" class="textcenter">
                  <s:if test="page.hasPrePage">
                  <a href="search.action?currentPage=${currentPage-1}">上一页</a>
                  </s:if>
                  <s:else>
                  <em>上一页</em>
                  </s:else>
                  </td>
                  <c:forEach var="pg" begin="1" end="${page.totalPage}">
                  <td width="20" class="textcenter"><a href="search.action?currentPage=${pg}"><c:out value="${pg}"/></a></td>
                  </c:forEach>                 
                  <td width="75" class="textcenter">
                  <s:if test="page.hasNextPage">
                  <a href="search.action?currentPage=${currentPage+1}">下一页</a>
                  </s:if>
                  <s:else>
                  <em>下一页</em>
                  </s:else>
                  </td>
                  <td width="144" class="textleft"><a href="search.action?currentPage=${page.totalPage}">末页</a></td>
                  <td width="117" class="textright">第<s:property value="page.currentPage"/>页 共
                    <s:property value="page.totalPage"/>页</td>
                 </tr>
              </table>
  		</body>
</html>

 

<script type="text/javascript">
	window.οnlοad=function(){
	var page=document.getElementById("page").value;
	var tb=document.getElementById("tb");
	var ulen=tb.getElementsByTagName("a").length;					  
	for(var i=0;i<ulen;i++){
		var url=tb.getElementsByTagName("a")[i].href;		
		url=url+'&everyPage='+page;
		tb.getElementsByTagName("a")[i].href=url;
		}
	};
	document.getElementById("page").οnchange=function(){
		var page=this.value;
		var tb=document.getElementById("tb");
		var ulen=tb.getElementsByTagName("a").length;					  
		for(var i=0;i<ulen;i++){
			var url=tb.getElementsByTagName("a")[i].href;		
			url=url.split('&everyPage=')[0]+'&everyPage='+page;
			tb.getElementsByTagName("a")[i].href=url;
			}
	};
</script>   


action、spring、struts.xml配置情况

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="struts2" extends="struts-default">
		<!-- 用户注册 -->
		<action name="register" class="userRegister">	
		<result name="success">/UserRegister.jsp</result>
		<result name="input">/UserRegister.jsp</result></action>
		<!-- 查询分页 -->
		<action name="search" class="userSearch">	
		<result name="success">/UserSearch.jsp</result>
		<result name="input">/UserSearch.jsp</result></action>
	</package>
	<constant name="struts.ui.theme" value="simple" /> 
</struts> 


package com.BBS.action;

import java.util.List;

import com.BBS.page.Page;
import com.BBS.page.Result;
import com.BBS.pojo.UserSsh;
import com.BBS.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserSearch extends ActionSupport{
	
	private static final long serialVersionUID = 2L;
	
	private UserService userService;
	private Page page;
	private List<UserSsh> userList;
	private int currentPage;
	private int everyPage;
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<UserSsh> getUserList() {
		return userList;
	}

	public void setUserList(List<UserSsh> userList) {
		this.userList = userList;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	
	public int getEveryPage() {
		return everyPage;
	}

	public void setEveryPage(int everyPage) {
		this.everyPage = everyPage;
	}

	public String execute() throws Exception {	
		Page schPage = new Page();				//设置分页信息
		schPage.setCurrentPage(currentPage);	//设置当前页
		schPage.setEveryPage(everyPage);		//设置每页显示
		Result schresult=userService.seachUser(schPage);
		page=schresult.getPage();
		userList=schresult.getList();
		return "success";
		
	}

}


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
    "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

	<!-- 定义数据源 -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="url"
			value="jdbc:mysql://localhost:3306/MySQL">
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="zjx"></property>
	</bean>
	
	<!-- 定义SessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">
  					true
				</prop>
				<prop key="hibernate.format_sql">
  					true
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/BBS/pojo/UserSsh.hbm.xml</value></list>
		</property></bean>
		
	<!-- 定义HibernateTemplate -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>		<!--注入SessionFactory实例对象-->
		</property>
	</bean>
	
	<!-- 定义HibernateTransactionManager -->
	<bean id="hibernateTransactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>	<!--注入SessionFactory实例对象-->
		</property>
	</bean>
	
	<!--装配数据访问层-->
	<bean id="usersshDAOTarget" class="com.BBS.dao.UserSshDAOImpl">
		<property name="hibernateTemplate">
			<ref bean="hibernateTemplate"/>	<!--注入HibernateTemplate -->
		</property>
	</bean>
	
	<!-- 为事务代理bean注入事务管理器-->
	<bean id="userSshDAO"
		 class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager">
			<ref bean="hibernateTransactionManager"/>
		</property>
		<property name="transactionAttributes"><!-- 设置事务属性 -->
			<props>
			<!-- 所有的方法,采用required的事务策略 -->
			<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
		<property name="target"><!--为事务代理bean设置目标bean -->
			<ref bean="usersshDAOTarget"/>
		</property>
	</bean>
	
	<!--注入DAO层-->
	<bean id="userService" class="com.BBS.service.UserServiceImpl">
		<property name="userSshDAO" ref="userSshDAO"></property>
	</bean>
	
	<!--注册Action-->
	<bean id="userRegister" class="com.BBS.action.UserRegister" scope="prototype">
		<property name="userService" ref="userService"></property>
	</bean>
	
	<!--查询Action-->
	<bean id="userSearch" class="com.BBS.action.UserSearch" scope="prototype">
		<property name="userService" ref="userService"></property>
	</bean>
	
	</beans>


  分页类

package com.BBS.page;

public class Page {
	// 每页显示数量
	private int everyPage;
	// 总记录数
	private int totalCount;
	// 总页数
	private int totalPage;
	// 当前页
	private int currentPage;
	// 起始点
	private int beginIndex;
	// 是否有上一页
	private boolean hasPrePage;
	// 是否有下一页
	private boolean hasNextPage;

	public Page(int everyPage, int totalCount, int totalPage, int currentPage,
			int beginIndex, boolean hasPrePage, boolean hasNextPage) {
		this.everyPage = everyPage;
		this.totalCount = totalCount;
		this.totalPage = totalPage;
		this.currentPage = currentPage;
		this.beginIndex = beginIndex;
		this.hasPrePage = hasPrePage;
		this.hasNextPage = hasNextPage;
	}

	//构造函数,默认
	public Page(){}
	
	//构造方法,对所有属性进行设置
	
	
	public int getEveryPage() {
		return everyPage;
	}

	public void setEveryPage(int everyPage) {
		this.everyPage = everyPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getBeginIndex() {
		return beginIndex;
	}

	public void setBeginIndex(int beginIndex) {
		this.beginIndex = beginIndex;
	}

	public boolean isHasPrePage() {
		return hasPrePage;
	}

	public void setHasPrePage(boolean hasPrePage) {
		this.hasPrePage = hasPrePage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

}


 

package com.BBS.page;
/*
 * 分页信息辅助类
 */
public class PageUtil {
	
	public static Page createPage(int everyPage,int totalCount,int currentPage) {
		everyPage = getEveryPage(everyPage);
		currentPage = getCurrentPage(currentPage);
		int totalPage = getTotalPage(everyPage, totalCount);
		int beginIndex = getBeginIndex(everyPage, currentPage);
		boolean hasPrePage = getHasPrePage(currentPage);
		boolean hasNextPage = getHasNextPage(totalPage, currentPage);
		return new Page(everyPage, totalCount, totalPage, currentPage,
				beginIndex, hasPrePage,  hasNextPage);
	}
	
	public static Page createPage(Page page,int totalCount) {
		int everyPage = getEveryPage(page.getEveryPage());
		int currentPage = getCurrentPage(page.getCurrentPage());
		int totalPage = getTotalPage(everyPage, totalCount);
		int beginIndex = getBeginIndex(everyPage, currentPage);
		boolean hasPrePage = getHasPrePage(currentPage);
		boolean hasNextPage = getHasNextPage(totalPage, currentPage);
		return new Page(everyPage, totalCount, totalPage, currentPage,
				beginIndex, hasPrePage,  hasNextPage);
	}
	
	//设置每页显示记录数
	public static int getEveryPage(int everyPage) {
		return everyPage == 0 ? 10 : everyPage;
	}
	
	//设置当前页
	public static int getCurrentPage(int currentPage) {
		return currentPage == 0 ? 1 : currentPage;
	}
	
	//设置总页数,需要总记录数,每页显示多少
	public static int getTotalPage(int everyPage,int totalCount) {
		int totalPage = 0;
		if(totalCount % everyPage == 0) {
			totalPage = totalCount / everyPage;
		} else {
			totalPage = totalCount / everyPage + 1;
		}
		return totalPage;
	}
	
	//设置起始点,需要每页显示多少,当前页
	public static int getBeginIndex(int everyPage,int currentPage) {
		return (currentPage - 1) * everyPage;
	}
	
	//设置是否有上一页,需要当前页
	public static boolean getHasPrePage(int currentPage) {
		return currentPage == 1 ? false : true;
	}
	
	//设置是否有下一个,需要总页数和当前页
	public static boolean getHasNextPage(int totalPage, int currentPage) {
		return currentPage == totalPage || totalPage == 0 ? false : true;
	}
	
}

 

package com.BBS.page;

import java.util.List;

public class Result {
	private Page page;			//分页信息
	private List list;			//列表操作
	public Page getPage() {		//获取分页信息
		return page;
	}
	public void setPage(Page page) {	//设置分页信息
		this.page = page;
	}
	public List getList() {		//获取列表信息
		return list;
	}
	public void setList(List list) {	//设置列表信息
		this.list = list;
	}
}

 

业务实现类和DAO实现类(核心)

package com.BBS.service;

import java.util.List;

import com.BBS.dao.UserSshDAO;
import com.BBS.page.Page;
import com.BBS.page.PageUtil;
import com.BBS.page.Result;
import com.BBS.pojo.UserSsh;

public class UserServiceImpl implements UserService{
	
	private UserSshDAO userSshDAO;								//用户DAO接口引用
	public void setUserSshDAO(UserSshDAO userSshDAO) {
		this.userSshDAO = userSshDAO;
	}
	public boolean addUser(UserSsh userSsh) {					//增加用户
		String username = userSsh.getUserName();				//获得用户名
		if(userSshDAO.queryByUsername(username) == null) {		//判断用户名是否被占用
			userSshDAO.saveUser(userSsh);						//保存用户
			return true;						//保存成功
		}else{
			return false;						//保存失败
		}
	}
	public Result seachUser(Page page){
		page=PageUtil.createPage(page,userSshDAO.AllUserCount());
		Result result=new Result();
		result.setList(userSshDAO.findPageUser(page));
		result.setPage(page);		
		return result;
		
	}
}

 

package com.BBS.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.BBS.page.Page;
import com.BBS.pojo.UserSsh;
import org.hibernate.Query;
import org.hibernate.Session;

public class UserSshDAOImpl extends HibernateDaoSupport implements UserSshDAO{
	
	public void saveUser(UserSsh userssh) {				//保存用户方法
		this.getHibernateTemplate().save(userssh);
	}
	
	public UserSsh queryByUsername(String username) {	//根据用户名查找
		@SuppressWarnings("unchecked")
		List<UserSsh> list = this.getHibernateTemplate().
				find("from UserSsh where userName = ?",username);
		if(list.size() == 0){							//判断查询集合是否为空
			return null;
		}else {
			return list.get(0);							//返回第一个用户
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<UserSsh> findPageUser(Page page){				 	//查询分页用户
		Session session = getSession();
		Query query = session.createQuery("from UserSsh");	//执行查询
		query.setFirstResult(page.getBeginIndex());			//设置分页信息
		query.setMaxResults(page.getEveryPage());
		return query.list();								//返回查询结果
		
	}
	public int AllUserCount(){
		List<UserSsh> list = this.getHibernateTemplate().
				find("from UserSsh");
		return list.size();
	}
	
}


其关键在于,Action将分页类传递入DAO,DAO查询结果,Service将计算好的分页类和查询结果返回,其页面中加入了JQuery(表格隔行变色)和JS(点击超链接将文本框内容传入Action)。

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值