我用的s2sh整合做的一个项目,用hibernateTemplate做的分页,然后在pagelist.jsp中展现分页后的效果,正确,但是我来回点击上一页,下一页,来回点击几次,然后再点击下一页或者上一页就很慢了,等好久页面都卡那不动了!连续访问pagelist.jsp几次之后也不行了。我用的是tomcat6.0x,其他界面都很顺畅可以!
下面是pagelist的jspp代码:<%@ page language="java" import="java.util.*,com.hotel.action.*,com.hotel.model.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>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>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body bgcolor="#D2B48C">
<HR align="LEFT" size="4">
<br>
<br>
<br>
<br>
<table width="800"border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
<tr>
<td >id</td>
<td>用户名</td>
<td>密码</td>
<td>手机号码</td>
<td>邮箱</td>
<td>注册时间</td>
<td>删除用户</td>
</tr>
<s:iterator value="p.list">
<tr>
<td>
<s:property value="id"/>
</td>
<td>
<s:property value="username"/>
</td>
<td>
<s:property value="password"/>
</td>
<td>
<s:property value="phone"/>
</td>
<td>
<s:property value="eamil"/>
</td>
<td>
<s:property value="registerdate"/>
</td>
<td><a href="/Hotel/admin/user_delete.action?id=<s:property value="id"/>" onclick= "return confirm('确认删除')">删除用户</a></td>
</tr>
</s:iterator></table><br>
<br>
<tr>
<td width="13%">
共
<s:property value="p.totalPage"/>
页
</td>
<td width="17%">
共
<s:property value="p.allRow"/>
条记录
</td>
<td width="15%">
当前第
<s:property value="p.currentPage"/>
页
</td>
<td colspan="4" width="55%">
<s:if test="%{p.currentPage == 1}">第一页 上一页 </s:if>
<s:else>
<a href="admin/pagelist.action?page=1">第一页 </a>
<a href="admin/pagelist.action?page=<s:property value="%{p.currentPage-1}"/>">上一页 </a>
</s:else>
<s:if test="%{p.currentPage != p.totalPage}">
<a href="admin/pagelist.action?page=<s:property value="%{p.currentPage+1}"/>">下一页 </a>
<a href="admin/pagelist.action?page=<s:property value="p.totalPage"/>">最后一页</a>
</s:if>
<s:else>下一页 最后一页</s:else>
</td>
</tr> <br>
<br>
<br>
<HR align="LEFT" size="4">
</body>
</html>
下面是pagelist的jspp代码:<%@ page language="java" import="java.util.*,com.hotel.action.*,com.hotel.model.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>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>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body bgcolor="#D2B48C">
<HR align="LEFT" size="4">
<br>
<br>
<br>
<br>
<table width="800"border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
<tr>
<td >id</td>
<td>用户名</td>
<td>密码</td>
<td>手机号码</td>
<td>邮箱</td>
<td>注册时间</td>
<td>删除用户</td>
</tr>
<s:iterator value="p.list">
<tr>
<td>
<s:property value="id"/>
</td>
<td>
<s:property value="username"/>
</td>
<td>
<s:property value="password"/>
</td>
<td>
<s:property value="phone"/>
</td>
<td>
<s:property value="eamil"/>
</td>
<td>
<s:property value="registerdate"/>
</td>
<td><a href="/Hotel/admin/user_delete.action?id=<s:property value="id"/>" onclick= "return confirm('确认删除')">删除用户</a></td>
</tr>
</s:iterator></table><br>
<br>
<tr>
<td width="13%">
共
<s:property value="p.totalPage"/>
页
</td>
<td width="17%">
共
<s:property value="p.allRow"/>
条记录
</td>
<td width="15%">
当前第
<s:property value="p.currentPage"/>
页
</td>
<td colspan="4" width="55%">
<s:if test="%{p.currentPage == 1}">第一页 上一页 </s:if>
<s:else>
<a href="admin/pagelist.action?page=1">第一页 </a>
<a href="admin/pagelist.action?page=<s:property value="%{p.currentPage-1}"/>">上一页 </a>
</s:else>
<s:if test="%{p.currentPage != p.totalPage}">
<a href="admin/pagelist.action?page=<s:property value="%{p.currentPage+1}"/>">下一页 </a>
<a href="admin/pagelist.action?page=<s:property value="p.totalPage"/>">最后一页</a>
</s:if>
<s:else>下一页 最后一页</s:else>
</td>
</tr> <br>
<br>
<br>
<HR align="LEFT" size="4">
</body>
</html>
解决方案 »
- 紧急!下午答辩!我用java语言,SSH框架做的一个小系统,联网的情况下可以运行,不联网就运行不了是为什么
- java后台获取viewstate
- 那位用过myeclipse 的那个spring explore
- springmvc
- 问一下sqlserver2000和java数据类型的关系
- jsp怎么实现同一个帐户不能同时登陆系统
- JSP中根据IP求出地址
- 菜鸟好急,关于struts2 页面传值给action的问题。。
- 如何在jsp中控制数据库。。。,请教!
- tomcat服务器端下载的.doc文件乱码(有的乱码,有的不乱)
- forward跳转问题?
- 怎么实现密码输入错误三次 然后给他提示让他找管理员功能 弹出一个框即可 万分感激
如果是mysql 的话 且有navicate for mysql 的话 打开工具 服务器监控 能看到、或者sql 语句:
show processlist;
import java.util.List;import javax.annotation.Resource;import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;import com.hotel.dao.PageDao;
import com.hotel.dao.UserDao;
import com.hotel.model.Page;
import com.hotel.model.User;import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;@Component("pageDao")
public class PageDaoImpl implements PageDao{
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
} @Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public List<User> getListForPage(final String hql, final int offset,final int length) {
@SuppressWarnings("unchecked")
List<User> list = getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List<User> list = query.list();
return list;
}
});
return list;
}
public int getCount(String hql) {
Query q = getHibernateTemplate().getSessionFactory().openSession().createQuery(hql);
return Integer.parseInt(q.list().get(0).toString());
}
public Page queryForPage(int pageSize, int page) {
String hql = "select count(*) from User";
int count =this.getCount(hql); // 总记录数
int totalPage = Page.countTotalPage(pageSize, count); // 总页数
int offset = Page.countOffset(pageSize, page); // 当前页开始记录
int length = pageSize; // 每页记录数
int currentPage = Page.countCurrentPage(page);
List<User> list = this.getListForPage("from User", offset, length); // 该分页的记录
// 把分页信息保存到Bean中
Page p = new Page();
p.setPageSize(pageSize);
p.setCurrentPage(currentPage);
p.setAllRow(count);
p.setTotalPage(totalPage);
p.setList(list);
p.init();
return p;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.hotel"/>
<!--
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
-->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean> <bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.hotel.model.User</value>
<value>com.hotel.model.Page</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean> <bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config>
<aop:pointcut expression="execution(public * com.hotel.service.*.*(..))" id="pointcutMethod"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutMethod"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
public int getCount(String hql) {
Query q = getHibernateTemplate().getSessionFactory().openSession().createQuery(hql);
return Integer.parseInt(q.list().get(0).toString());
}
openSession后session是需要手动关闭的,我把上面的一行写成:
Query q = hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(hql);就OK了
getCurrentSession会自动的关闭session。