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 ListgetAll(int page) { List 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"%>Insert title here 添加用户用户列表
<%@page import="com.hanqi.utility.USer"%><%@page import="java.util.List"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here <%Listlist = (List )request.getAttribute("selectuser") ;for(USer us : list ){ out.print(us + " 【删除】" +" 【修改】" +" ") ;}int pages = Integer.parseInt(request.getParameter("pages")) ;%><% if(list.size()==0){response.getWriter().write("页码超出范围") ; response.setHeader("refresh", "3;url=selectUSer?pages=1") ;}else{ %><%%> 上一页 下一页<%} %>
Struts文件配置
/WEB-INF/pages/fail.jsp /WEB-INF/pages/selectUSer.jsp
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")) ; //调用查询的方法 Listlist = new UserService().getAll(page); hsr.setAttribute("pages", page+1); hsr.setAttribute("selectuser", list); rtn = "success" ; }catch(Exception e) { e.printStackTrace(); } return rtn; }