PageHelper查询分页

页面加载,请求ajax,查询结果分页显示(解析数据和页码)。增加模糊条件查询,同样也要分页显示。

jar包:

applicationContext-mybatis.xml:



     
     
	
	    
	
     
     
     
      
         
         
         
         
    
    
    
    
        
    
        
    
        
        
       
            
              
                
                  
                  
                  
                
              
            
             
    
 
    
    
        
    
     
  
       
            
              
                
                  
                  
                  
                
              
            
             
    
 
    
    
        
    
     

list.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>





<%
	pageContext.setAttribute("ctx", request.getContextPath());
%>
用户管理




序号 账号 密码 性别 年龄

ajax返回的通用bean:

public class Msg {

	// 状态码 100-成功 200-失败
	private int code;
	// 提示信息
	private String msg;
	//用户返回给浏览器的数据
	private Map extend = new HashMap();
	//处理成功
	public static Msg success() {
		Msg result = new Msg();
		result.setCode(100);
		result.setMsg("处理成功!");
		return result;	
	}
	//处理失败
	public static Msg fail() {
		Msg result  =new Msg();
		result.setCode(200);
		result.setMsg("处理失败");
		return result;
	}
	
	//链式操作
	public Msg add(String key,Object value) {
		this.getExtend().put(key, value);
		return this;
	}
	
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public Map getExtend() {
		return extend;
	}
	public void setExtend(Map extend) {
		this.extend = extend;
	}
}

controller方法:

@RequestMapping("/listWithJson")
	@ResponseBody
	public Msg listWithJson(@RequestParam(value="pn",defaultValue="1") Integer pn) {
		PageHelper.startPage(pn,2);
		List list = userService.list();
		PageInfo pageInfo = new PageInfo(list);
		return Msg.success().add("pageInfo", pageInfo);
	}

@RequestMapping("/search")
	@ResponseBody
	public Msg search(@RequestParam(value="pn",defaultValue="1") Integer pn,
			User user) {
		PageHelper.startPage(pn, 2);
		List list = userService.searchUser(user);
		System.out.println("有"+list+"个");
		PageInfo pageInfo = new PageInfo(list);
		return Msg.success().add("pageInfo", pageInfo);
	}
返回到页面:
@RequestMapping("/userList3")
	public String userList3(@RequestParam(value="pn",defaultValue="1") Integer pn,
			Model model) 
	{
		PageHelper.startPage(pn, 2);
		List list = userService.list();
		PageInfo page = new PageInfo(list);
		model.addAttribute("pageInfo", page);
		return "userList";
	}

   PageInfo page = new PageInfo(emps,5);分页条

//解析显示分页条,点击分页要能去下一页....
		function build_page_nav(result){
			//page_nav_area
			$("#page_nav_area").empty();
			var ul = $("
    ").addClass("pagination"); //构建元素 var firstPageLi = $("
  • ").append($("").append("首页").attr("href","#")); var prePageLi = $("
  • ").append($("").append("«")); if(result.extend.pageInfo.hasPreviousPage == false){ firstPageLi.addClass("disabled"); prePageLi.addClass("disabled"); }else{ //为元素添加点击翻页的事件 firstPageLi.click(function(){ to_page(1); }); prePageLi.click(function(){ to_page(result.extend.pageInfo.pageNum -1); }); } var nextPageLi = $("
  • ").append($("").append("»")); var lastPageLi = $("
  • ").append($("").append("末页").attr("href","#")); if(result.extend.pageInfo.hasNextPage == false){ nextPageLi.addClass("disabled"); lastPageLi.addClass("disabled"); }else{ nextPageLi.click(function(){ to_page(result.extend.pageInfo.pageNum +1); }); lastPageLi.click(function(){ to_page(result.extend.pageInfo.pages); }); } //添加首页和前一页 的提示 ul.append(firstPageLi).append(prePageLi); //1,2,3遍历给ul中添加页码提示 $.each(result.extend.pageInfo.navigatepageNums,function(index,item){ var numLi = $("
  • ").append($("").append(item)); if(result.extend.pageInfo.pageNum == item){ numLi.addClass("active"); } numLi.click(function(){ to_page(item); }); ul.append(numLi); }); //添加下一页和末页 的提示 ul.append(nextPageLi).append(lastPageLi); //把ul加入到nav var navEle = $("").append(ul); navEle.appendTo("#page_nav_area"); }

    你可能感兴趣的:(前端,ssm)