dao层代码

package com.hanqi.dao;

import java.util.ArrayList;
import java.util.List; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry; import com.hanqi.utility.USer; public class USerDAO
{
private Configuration cfg = null ;
private ServiceRegistry sr = null ; private SessionFactory sf = null ;
private Session se = null ;
Transaction ts = null ; public USerDAO()
{
//初始化Hibernate
cfg = new Configuration().configure() ; sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build() ; } public void init()
{
sf = cfg.buildSessionFactory(sr) ; se = sf.openSession() ; ts = se.beginTransaction() ;
} public void destory()
{
ts.commit() ; se.close() ; sf.close() ;
} //查询列表
public List<USer> getAll(int page)
{
List<USer> list = new ArrayList<>() ; init() ; //设置每页两行及起始页
list = se.createQuery("from USer").setMaxResults(2)
.setFirstResult((page-1)*2).list() ; destory(); return list;
}
}

jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="addUSer">添加用户</a>
<br><br>
<a href="selectUSer?pages=1" >用户列表</a>
</body>
</html>
<%@page import="com.hanqi.utility.USer"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% List<USer> list = (List<USer>)request.getAttribute("selectuser") ; for(USer us : list )
{
out.print(us + "<a href='deleteuser?user_id="+us.getUser_id()+"'> 【删除】</a>"
+"<a href='modify?userid="+us.getUser_id()+"'> 【修改】</a>"
+"<br>") ;
}
int pages = Integer.parseInt(request.getParameter("pages")) ; %> <% if(list.size()==0){response.getWriter().write("页码超出范围") ;
response.setHeader("refresh", "3;url=selectUSer?pages=1") ;}else{ %>
<%%>
<a href="selectUSer?pages=<%=pages-1%>">上一页</a>
<a href="selectUSer?pages=<%=pages+1%>">下一页</a>
<%} %>
</body>
</html>

Struts文件配置

<!-- 查询全部 -->
<action name="selectUSer" class="com.hanqi.action.USerAction" method="selectUSer"> <result name="fail">/WEB-INF/pages/fail.jsp</result> <result name="success">/WEB-INF/pages/selectUSer.jsp</result> </action>

service层方法

public String selectUSer()
{
String rtn = "fail" ; try
{//得到原生request
HttpServletRequest hsr = ServletActionContext.getRequest() ; int page = Integer.parseInt(hsr.getParameter("pages")) ; //page = Integer.parseInt(hsr.getParameter("pagess")) ;
//调用查询的方法
List<USer> list = new UserService().getAll(page); hsr.setAttribute("pages", page+1); hsr.setAttribute("selectuser", list); rtn = "success" ;
}catch(Exception e)
{
e.printStackTrace();
} return rtn;
}
05-11 00:41