DAO层
/**
* 分页查询全部员工,获取总记录数
*/
public int totalPage(String className);
/**
* 分页查看,查看首页
*/
public List<Employee> selectByPage(String className,int pageNo,int pageSize);

  DaoImp里面

 private EmployeeDao employeeDao;

    public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
} /**
* 员工列表分页查询,查询总页数
* @param className
* @return
*/
@Override
public int totalPage(String className) {
String hql="select count(*) from "+className;
Query query= this.getSession().createQuery(hql);
int total=Integer.parseInt(query.list().get(0).toString());
return total;
} /**
* 分页查询,查询首页
* @param className
* @param pageNo
* @param pageSize
* @return
*/
@SuppressWarnings("unchecked")
@Override
public List<Employee> selectByPage(String className, int pageNo, int pageSize) {
Query query=this.getSession().createQuery("from "+className);
query.setFirstResult((pageNo-1)*pageSize);//每页显示的第一条记录
query.setMaxResults(pageSize);//每页显示的记录数
return query.list();
}

  BIz层

 /**
* 分页查看,查看首页
*/
@SuppressWarnings("rawtypes")
public List<Employee> selectByPage(String className,int pageNo,int pageSize); /**
* 查看下一页
* @param line
* @param className
* @return
*/
public int searchRecordsNextPage(int line,String className);

  BIzImpl层

    private EmployeeDao employeeDao;

    public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
} /**
* 分页查询
* @param className
* @param pageNo
* @param pageSize
* @return
*/
@SuppressWarnings("rawtypes")
@Override
public List<Employee> selectByPage(String className, int pageNo, int pageSize) {
List list= employeeDao.selectByPage(className,pageNo,pageSize);
return list;
} /**
* 查看下一页
* @param pageSize
* @param className
* @return
*/
@Override
public int searchRecordsNextPage(int pageSize, String className) {
int total = employeeDao.totalPage(className);
int pageNo = total / pageSize;
if(total % pageSize>0){
pageNo++;
}
return pageNo;
}

  Action控制层

private Employee employee=new Employee();
@Override
public Employee getModel() {
return employee;
}
private EmployeeBiz employeeBiz; public void setEmployeeBiz(EmployeeBiz employeeBiz) {
this.employeeBiz = employeeBiz;
} /**
* 分页查看员工列表
*/
@SuppressWarnings({"unchecked","rawtypes"})
public String selectFirstPage(){
Map<String,Object> request= (Map<String, Object>) ActionContext.getContext().get("request");
try{
List list=employeeBiz.selectByPage("Employee",1,3);
int totalPage=employeeBiz.searchRecordsNextPage(3,"Employee");
request.put("list",list);
request.put("totalPage",totalPage);
request.put("currentPage",1);
}catch(Exception e){
e.printStackTrace();
}
return "selectFirstPage";
}
/**
* 分页查找下一页
*/
@SuppressWarnings("rawtypes")
public String selectNextPage(){
HttpServletRequest request= ServletActionContext.getRequest();
int next=Integer.parseInt(request.getParameter("page"));
try{
List list=employeeBiz.selectByPage("Employee",next,3);
int total=employeeBiz.searchRecordsNextPage(3,"Employee");
request.setAttribute("list",list);
request.setAttribute("currentPage",next);
request.setAttribute("totalPage",total);
}catch(Exception e){
e.printStackTrace();
}
return "selectNextPage";
}

  前端div接收参数

<div>
第${currentPage }页
<input type="hidden" id="tpage" name="tpage"
value="${totalPage} "></input>
<c:if test="${currentPage>1 }">
<input type="button" value="首页" onclick="show(1)"></input>
<input type="button" value="上一页" onclick="show(${currentPage-1})"></input>
</c:if>
<c:if test="${currentPage<totalPage }">
<input type="button" value="下一页" onclick="show(${currentPage+1},${totalPage })"></input>
<input type="button" value="尾页" onclick="show(${totalPage})"></input>
</c:if>
共${totalPage }页
</div>
</div>

  前端javascript里

 function show(next,total){
if (next<=0) {
alert("已经是首页了");
return;
}
if (next>total) {
alert("已经是尾页了");
return;
}
document.forms[0].action="employeeselectNextPage?page="+next;
document.forms[0].submit();
}

  注意:1,在body里面要想实现分页要把body里面的form表单加上

<form id="myForm" name="myForm" method="post">

2,如果使用的是mysql需要修改hibernate.cfg.xml里面的

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

  3,在struts.xml文件里面需要修改name里面的是第一页和下一页的方法

<!-- Employee action -->
<action name="employee*" class="EmployeeAction" method="{1}">
<result name="selectFirstPage">/employee.jsp</result>
<result name="selectNextPage">/employee.jsp</result>
<result name="add" type="redirectAction">employeeselectFirstPage</result>
<result name="updateEmp">/employee_update.jsp</result>
<result name="update" type="redirectAction">employeeselectFirstPage</result>
<result name="delete" type="redirectAction">employeeselectFirstPage</result>
<result name="error">/error.jsp</result>
</action>

 

SSH框架分页的更多相关文章

  1. 分页技术框架(Pager-taglib)学习二(SSH数据库分页)

    一.Pager-taglib数据库分页前提    Pager-taglib分页标签也可以实现数据库分页,与页面分页不同的是需要给后台传两个参数,一个是pageNo(当前页数)或pageOffset(偏 ...

  2. 使用maven搭建SSH框架实现登陆、列表查询分页

    SSH框架:struts2 + spring + hibernate web层:struts2+jsp service层:javaBean dao层:hibernate spring:管理Action ...

  3. 管理系统-------------SSH框架书写登录和显示用户

    一.思路的穿插. web.xml中的配置找到--->application.xml---->找到对应的Action---->找到struts.xml----->在去找actio ...

  4. 学习SSH框架

    1.SSH框架的认知 在做相关的java的网页的开发制作时,良好的层次分解是十分有比要的,所以我们在云涌第三方的框架之下来简化还有名了我们相关的网站的开发. SSH框架实则为Struct + spri ...

  5. 项目分享:通过使用SSH框架的公司-学员关系管理系统(CRM)

    ----------------------------------------------------------------------------------------------[版权申明: ...

  6. 项目:《ssh框架综合项目开发视频》-视频目录和第六天的EasyUI简单讲解

    4 练习使用技术: Struts2 + hibernate5.x + spring4.x + mysql数据库 1 crm:customer relational manager,客户关系管理 2 c ...

  7. SSH框架结合案例构建配置

    ssh框架概述 SSH是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架.区别于 Secure Shell . 集成SSH框架的系统从职责上分 ...

  8. 简化SSH框架的整合

    一.开发环境: (1)    OS:Windows 7 (2)    DB:MySql 5.1.6 (3)    JDK:1.8.0_17 (4)    Server:Apache Tomcat 8. ...

  9. SSH框架整合

    SSH框架整合 一.原理图 action:(struts2) 1.获取表单的数据 2.表单的验证,例如非空验证,email验证等 3.调用service,并把数据传递给service Service: ...

随机推荐

  1. BUAA Summer Practice 2017 #1 字符串专场

    https://vjudge.net/contest/262753#overview C - Regular Number HDU - 5972 bitset temp, temp[i]=1表示 此前 ...

  2. 软件测试实验二----selenium、katalon、junit

    1.安装firefox和seleniumIDE.katalon 安装按成后在Firefox中有seleniumIDE.katalon的图标 2.使用katalon导出测试脚本 点击katalon的插件 ...

  3. leetcode 最后一个单词的长度 python

    class Solution: def lengthOfLastWord(self, s): """ :type s: str :rtype: int "&qu ...

  4. VB代码收集

    1.随机获取5位验证码? 需求: 创建一个Label1:名称为随机验证码生成 创建一个Label2:名称为为空,属性BorderStyle=1 创建一个CommandButton:名称为获取随机码 代 ...

  5. MyTests

    目录 About Tests Selenium自动化测试 Pyppeteer Explain About Tests 扯淡!测试之瞎扯淡 Selenium自动化测试 什么是Selenium? Sele ...

  6. 【题解】Luogu P5251 [LnOI2019]第二代图灵机

    原题传送门 前置芝士:珂朵莉树 珂朵莉树的主要功能是区间赋值 这道题还算明显(操作2) 一开始看见这题觉得很毒瘤,但仔细想想发现颜色和数字之间没有什么关系 我们一共要维护三个东西: 1.区间和:树状数 ...

  7. Http协议&Servlet

    http协议 针对网络上的客户端 与 服务器端在执行http请求的时候,遵守的一种规范. 其实就是规定了客户端在访问服务器端的时候,要带上哪些东西, 服务器端返回数据的时候,也要带上什么东西. 版本 ...

  8. webform运行时弹出JavaScript的alert窗口

    https://stackoverflow.com/questions/9720143/asp-net-web-application-message-box Or create a method l ...

  9. .Net dependent configuration

    error info: 解决方案:在.exe.config文件中配置Newtonsoft.Json所用版本 <runtime> <assemblyBinding xmlns=&quo ...

  10. Go 参数传递是传值还是传引用

    什么是传值(值传递)? 传值的意思是:函数传递的总是原来这个东西的一个副本.一个副拷贝.比如我们传递一个 int 类型的参数,传递 的其实这个参数的一个副本:传递一个指针类型的参数,其实传递的是这个指 ...