SSH框架分页
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框架分页的更多相关文章
- 分页技术框架(Pager-taglib)学习二(SSH数据库分页)
一.Pager-taglib数据库分页前提 Pager-taglib分页标签也可以实现数据库分页,与页面分页不同的是需要给后台传两个参数,一个是pageNo(当前页数)或pageOffset(偏 ...
- 使用maven搭建SSH框架实现登陆、列表查询分页
SSH框架:struts2 + spring + hibernate web层:struts2+jsp service层:javaBean dao层:hibernate spring:管理Action ...
- 管理系统-------------SSH框架书写登录和显示用户
一.思路的穿插. web.xml中的配置找到--->application.xml---->找到对应的Action---->找到struts.xml----->在去找actio ...
- 学习SSH框架
1.SSH框架的认知 在做相关的java的网页的开发制作时,良好的层次分解是十分有比要的,所以我们在云涌第三方的框架之下来简化还有名了我们相关的网站的开发. SSH框架实则为Struct + spri ...
- 项目分享:通过使用SSH框架的公司-学员关系管理系统(CRM)
----------------------------------------------------------------------------------------------[版权申明: ...
- 项目:《ssh框架综合项目开发视频》-视频目录和第六天的EasyUI简单讲解
4 练习使用技术: Struts2 + hibernate5.x + spring4.x + mysql数据库 1 crm:customer relational manager,客户关系管理 2 c ...
- SSH框架结合案例构建配置
ssh框架概述 SSH是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架.区别于 Secure Shell . 集成SSH框架的系统从职责上分 ...
- 简化SSH框架的整合
一.开发环境: (1) OS:Windows 7 (2) DB:MySql 5.1.6 (3) JDK:1.8.0_17 (4) Server:Apache Tomcat 8. ...
- SSH框架整合
SSH框架整合 一.原理图 action:(struts2) 1.获取表单的数据 2.表单的验证,例如非空验证,email验证等 3.调用service,并把数据传递给service Service: ...
随机推荐
- quill富文本编辑器 API
//1. 从第三个开始删除,删除4个 // console.log(this.quill.deleteText(2, 4)); // 12345678 1278 // 2.(返回对象)返回从第三个开始 ...
- Redis学习-主从复制、哨兵
主从复制 官方文档:https://redis.io/topics/replication Redis中的主从复制,也就是Master-Slave模型,有以下特点 Master可以拥有多个slave ...
- kbmmemtable sorton 报错 : List index out of bounds
同一数据集,不同的排序条件,有的可以,但某一条件,却能100%重现报错. procedure TkbmIndex.InternalFastQuickSort(const L,R:Integer); v ...
- MTK-TP(触屏)解读一
MTK中的TP代码结构并不复杂,相比于其他的系统更为的简单些.它使用的是input子系统,通过该系统来上报触摸按键. 首先我们来看看TP的文件夹下的各代码文件的功能. 文件名 具体功能 关系文件 tp ...
- Eclipse中启动Tomcat报错:[There is insufficient memory for the Java Runtime Environment to continue.]的解决方案
1,报错截图 2,报错信息 五月 08, 2018 9:57:58 上午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [Se ...
- linux 安装软件三种方法
引言 在ubuntu当中,安装应用程序我所知道的有三种方法,分别是apt-get,dpkg安装deb和make install安装源码包三种.下面针对每一种方法各举例来说明. apt-get方法 使用 ...
- JavaScript 中的 FileReader
在不经过服务器的时候,本地预览照片,当确定以后再上传 它是H5提供的构造函数 用法: <input type='file'> <img src='' alt=''> <s ...
- Domain logic approachs
1.transaction script(事务脚本) 概述: 很多企业应用可以看成一系列的事务,每一个事务可以通过使用一个Transaction Script来处理. 用法: 使用Transactio ...
- P3321 [SDOI2015]序列统计
思路 首先有个挺显然的DP \[ dp[i][(j*k)\%m]+=dp[i-1][j]\times dp[i-1][k] \] 想办法优化这个DP 这个dp也可以写成这样 \[ dp[i][j]=\ ...
- git误commit大文件导致不能push问题解决
git push时终端报错: error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Ent ...