首页 > 代码库 > Struts+Hibernate+jsp页面 实现分页
Struts+Hibernate+jsp页面 实现分页
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页面,这里通过a标签的get方式传递页码
<%@ 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>
跳转到显示界面
1 <%@page import="com.hanqi.utility.USer"%> 2 <%@page import="java.util.List"%> 3 <%@ page language="java" contentType="text/html; charset=UTF-8" 4 pageEncoding="UTF-8"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title>10 </head>11 <body>12 <%13 14 List<USer> list = (List<USer>)request.getAttribute("selectuser") ;15 16 for(USer us : list )17 {18 out.print(us + "<a href=‘deleteuser?user_id="+us.getUser_id()+"‘> 【删除】</a>"19 +"<a href=‘modify?userid="+us.getUser_id()+"‘> 【修改】</a>"20 +"<br>") ;21 }22 int pages = Integer.parseInt(request.getParameter("pages")) ;23 24 %>25 26 <% if(list.size()==0){response.getWriter().write("页码超出范围") ;27 response.setHeader("refresh", "3;url=selectUSer?pages=1") ;}else{ %>28 <%%>29 <a href="selectUSer?pages=<%=pages-1%>">上一页</a>30 <a href="selectUSer?pages=<%=pages+1%>">下一页</a>31 <%} %>32 </body>33 </html>
Struts文件配置
1 <!-- 查询全部 -->2 <action name="selectUSer" class="com.hanqi.action.USerAction" method="selectUSer">3 4 <result name="fail">/WEB-INF/pages/fail.jsp</result>5 6 <result name="success">/WEB-INF/pages/selectUSer.jsp</result>7 8 </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; }
页码超出时,提示错误(通过jsp的List来判断)Hibernate提供的方法返回的List.size()为行数(最后一页可能少于行数),若为零则没有数据即可判断
Struts+Hibernate+jsp页面 实现分页
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。