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>
效果: