利用Ajax实现无刷新分页

利用Ajax实现无刷新分页,第1张

JSON:对象字符串(满足对象的定义规则)

var obj = {"name":"value","name":"value"};

JSON的使用

前端:

原生态js:JSON.parse(json) stringify() jQuery:parseJSON

后端

fastJSON

对象《----》JSON字符串 JSON.toJSONString(obj);

JSON.parseObject(str,对象.class);

jackJSON

ObjectMapper mapper = new ObjectMapper();

mapper.writeValueAsString(obj); mapper.readValue(str,对象.class);

AJAX的使用

let path = "${pageContext.request.servletContext.contextPath}";

无刷新页面语法规则:

$.ajax({ url:path+"/hell.do", type:"post",

data:{"username":"admin","password":"123"},

datatype:"json",

success:function(data){ //data 相关处理 } });

$.post(path+"/hello.do",{"username":"admin","password":"123"},function(data){

});

代码展示:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




无刷新分页





    
    
    
    
    
    /
    首页
    上一页
    下一页
    尾页
​
    
    

跳转的servlet界面:

package com.zking.cart.ht.servlet;
​
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
​
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
​
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zking.cart.biz.IGoodsBiz;
import com.zking.cart.biz.impl.GoodsBizImpl;
import com.zking.cart.entity.Goods;
import com.zking.cart.utils.GetUtils;
​
//后台显示数据的servlet
@WebServlet("/amdinGoodsList.do")
public class adminGoodsListServlet extends HttpServlet {
    
    private static final long serialVersionUID = 1L;
​
    /**
     * @see HttpServlet#doGet(HttpServletRequest request,
     * 
     *      HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request,
​
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }
​
    /**
     * @see HttpServlet#doPost(HttpServletRequest request,
     * 
     *      HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        // 1.设置请求和响应编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
​
        Integer pageIndex = 1;
        Integer pageSize = 3;
        // 2.处理请求
        String pIndex = request.getParameter("pageIndex");
​
        if (null != pIndex) {
            pageIndex = Integer.valueOf(pIndex);
        }
​
        Integer pageCount = new GetUtils().getTableCount("tb_goods", "gname", "");
        Integer pageMax = pageCount % pageSize != 0 ?pageCount / pageSize + 1 : pageCount / pageSize;
​
        // 3.处理请求(调用biz通过域对象保存)
        IGoodsBiz biz = new GoodsBizImpl();
​
        List adminListGoods = biz.queryGoodsAll(pageIndex, pageSize, "");
​
        // map集合保存
        Map maps = new HashMap();
        maps.put("adminListGoods", adminListGoods);
        maps.put("pageMax", pageMax);
​
        ObjectMapper mapper = new ObjectMapper();
        String writeValueAsString =mapper.writeValueAsString(maps);
​
        PrintWriter out = response.getWriter();
        out.write(writeValueAsString);
        out.flush();
        out.close();
    }
​
}
​

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/939793.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-17
下一篇 2022-05-17

发表评论

登录后才能评论

评论列表(0条)

保存