[原]Java web学习系列之 Java web开发中的Hibernate结合Servlet做分页

 首先配置Hibernate文件(HibernateSessionFactory)和实体类。

Hibernate.cfg.xml中的配置如下:

<hibernate-configuration>

<session-factory>
<property name="connection.username">sa</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;databaseName=GOODS
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="myeclipse.connection.profile">sal</property>
<property name="connection.password">sasa</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<mapping resource="org/clarck/model/entity/Sell.hbm.xml" />
<mapping resource="org/clarck/model/entity/Goods.hbm.xml" />
<mapping resource="org/clarck/model/entity/Selldetail.hbm.xml" />

</session-factory>

</hibernate-configuration>

接着就是dao类:

public class GoodsDAO {
public List<Goods> showGoods(int pageIndex ,int pageSize){

Session session=HibernateSessionFactory.getSession();
Query query=session.createQuery("from Goods");//hql查询语句保存到query中
query.setFirstResult((pageIndex-1)*pageSize).setMaxResults(pageSize);//按照页面显示数据的最大数进行分页设置
List<Goods> list=query.list();
session.close();
return list;
}
}

 接着配置Servlet:

<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ShowServlet</servlet-name>
<servlet-class>org.clarck.web.action.ShowServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ShowServlet</servlet-name>
<url-pattern>/ShowServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

这里的Servlet实现action的功能完成控制层的任务:

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
int pageIndex=1;
int pageSize=2;
String strIndex=request.getParameter("pageIndex");
if(strIndex==null){
pageIndex=1;
}
else {
pageIndex=Integer.parseInt(strIndex);//将得到的页面数据转换成Integer保存到pageIndex中
}
GoodsDAO goodsDAO=new GoodsDAO();
List<Goods> list=goodsDAO.showGoods(pageIndex, pageSize);//调用方法得到的数据封装到list
HttpSession session=request.getSession();//Servlet中没有隐式对象session,这里的session继承自HttpSession
session.setAttribute("pageIndex", pageIndex);//将页面数据保存到session中
session.setAttribute("pageList", list);//将list保存到session中
response.sendRedirect("index.jsp");//重定向到index.jsp页面
}

在index.jsp页面上面使用JSTL标签库,在这里尽量避免使用服务器脚本,目的是保证页面的安全性:

首先导入文件支持:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

接着就是显示读出的数据:

<body>
<table>
<c:forEach var="goods" items="${pageList}">
<tr>
<td>${goods.goodsId}</td>
<td>${goods.goodsNum}</td>
<td>${goods.goodsName}</td>
<td>${goods.price}</td>
<td>${goods.produceDatetime}</td>
<td>${goods.quality}</td>
<td>${goods.address}</td>
<td>${goods.state}</td>
</tr>
</c:forEach>
</table>
<a href="ShowServlet?pageIndex=${pageIndex-1}">上一页</a>
<a href="ShowServlet?pageIndex=${pageIndex+1}">下一页</a>
</body>

 

笔记记于 2010-9-1 10:56

你可能感兴趣的:(Hibernate)