2017/2/22 基于SpringMVC和Mybatis的分页实现(2)
来源:互联网 发布:swift开发windows程序 编辑:程序博客网 时间:2024/06/06 01:29
BaseController:
package com.chinook5.controller.base;import javax.servlet.http.HttpServletRequest;import com.chinook5.controller.converters.CustomDateConverter;import com.chinook5.util.PageData;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import org.springframework.web.servlet.ModelAndView;public class BaseController extends CustomDateConverter {private static final long serialVersionUID = 6357869213649815390L;/** * 得到PageData */public PageData getPageData(){return new PageData(this.getRequest());}/** * 得到ModelAndView */public ModelAndView getModelAndView(){return new ModelAndView();}/** * 得到request对象 */public HttpServletRequest getRequest() {HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();return request;}}
普通controller需要继承basecontroller:
@RequestMapping(value = "get_user") public @ResponseBody Map<String, Object> world(@RequestParam(value = "page", defaultValue = "1", required = true) Integer currentPage) throws Exception { Map<String,Object> map = new HashedMap(); PageData pd = this.getPageData(); Page p = new Page(); p.setPd(pd); p.setCurrentPage(currentPage); List<PageData> data = userInfoService.getUserInfoListPage(p); Page.PageStr(p); map.put("pageData",data); map.put("pageInfo",p); return map; }
service:
public List<PageData> getUserInfoListPage(Page p) throws Exception { return (List<PageData>)dao.findForList("UserInfoMapper.getUserInfoListPage",p); }
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="UserInfoMapper"> <select id="getUserInfoListPage" parameterType="page" resultType="pd"> SELECT * from userinfo </select></mapper>
user_list.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%><html><head> <title>Title</title> <script src="<%=basePath%>system/js/jquery-1.9.1/jquery.min.js" type="text/javascript"></script> <style> li{ list-style-type: none; float: left; padding-left: 20px; } th{ padding-left: 50px; } td{ text-align: center; } </style></head><body><input id="currentPage" type="hidden" value="${pd.page}"/><table id="t1"> <thead> <tr> <th>UserID</th> <th>UserName</th> <th>操作</th> </tr> </thead> <tbody id="tablelist"> </tbody></table><div style="text-align:center;" id="pagination"></div></body><script> $(function () { GetList(); }); function GetList() { var index = location.hash.indexOf("#!page="); var page = 1; if (index != -1) { page = location.hash.substring(index + 7); } if ($("#currentPage").val().length > 0) { page = $("#currentPage").val(); } toPage(page); $("#search").click(function () { toPage(1); location.hash = "!page=1"; }); } function toPage(page) { $.ajax({ url: '/user/get_user', type: 'post', data: { "page": page }, async: false, dataType: 'json', success: function (data) { var divList = $('#tablelist'); divList.empty(); if (data.pageData.length > 0) { //更新页码 location.hash = "!page=" + data.pageInfo.currentPage; //更新列表显示的数据 $("#currentPage").val(data.pageInfo.currentPage); } $.each(data.pageData, function (i, item) { var divContent = divContent + "<tr><td>" + item.UserID + "</td><td>" + item.UserName + "</td><td><a href='/user/" + item.UserID + "'>编辑</a></td></tr>"; divList.append(divContent); }); //更新分页字符串 $("#pagination").html(data.pageInfo.pageStr); } }); }</script></html>
效果:
0 0
- 2017/2/22 基于SpringMVC和Mybatis的分页实现(2)
- 2017/2/22 基于SpringMVC和Mybatis的分页实现(1)
- springmvc+Mybatis 分页查询的简单实现
- SpringMVC的mybatis分页
- 基于mybatis 自定义标签分页的实现
- 基于mybatis ajax 分页的实现
- mybatis分页实现2
- SpringMVC+MyBatis+MySQL实现分页
- 基于maven,springmvc+mybatis+pagehepler分页配置
- Spring+SpringMVC+Mybatis+PageHelper+laypage+Ajax实现的分页
- 基于SpringMVC 和MyBatis的实现省份城市之间的动态切换
- springmvc+mybatis+mysql的分页的查询和显示
- springMVC、myBatis的物理分页和高级查询
- springMVC、myBatis的物理分页和高级查询
- SpringMVC框架学习(2)--SpringMVC和Mybatis的整合
- MyBatis+springMVC+easyUI (dataGirl)实现分页
- SpringMVC+MyBatis分页插件简单实现
- SpringMVC+Mybatis+Oracle实现web分页
- leetcode No215. Kth Largest Element in an Array
- 同步、异步转化和任务执行
- SpringMVC 文件上传 MultipartFile、CommonsMultipartResolver
- WebGL 写入纹理
- Linux学习之———压缩命令
- 2017/2/22 基于SpringMVC和Mybatis的分页实现(2)
- Cookie中字符串分隔问题
- 字符串对比
- 如何用docker安装laravel开发环境
- 解决hyper-v 虚拟机 网速很慢问题
- /etc/sysctl.conf
- Java并发编程中CountDownLatch和CyclicBarrier的使用
- 1.初始化及header布局
- nivida 显卡下使用vnc+centos7