java web 分页技术_javaweb分页的后端实现
先上demo圖
servlet實(shí)現(xiàn)部分:
package servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.User;
import service.UserService;
@SuppressWarnings("serial")
@WebServlet(urlPatterns ="/user")
public class UserServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
this.doGet(req, resp);
}
/*
/* json {"users":[{"id":1,"name":"mercy","age":22,"sex":3},{"id":2,"name":"lily","age":23,"sex":2},{"id":3,"name":"mily","age":23,"sex":3},{"id":4,"name":"lucy","age":25,"sex":3},{"id":5,"name":"john","age":24,"sex":2}],"total":6}
*/
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) {
UserService service=new UserService();
String name=req.getParameter("name");
int page=Integer.parseInt(req.getParameter("page"));
int size=Integer.parseInt(req.getParameter("size"));
byte sex=Byte.parseByte(req.getParameter("sex"));
page=page-1;
List list=service.queryDataLimit(page, size, sex, name);
long count=service.queryAll(sex, name);
//build json
StringBuilder builder=new StringBuilder("{\"users\":[");
String results="";
if(!list.isEmpty()) {
for(User u:list) {
String result = "{\"id\":"+u.getId()+",\"name\":"+"\""+u.getName()+"\""+",\"age\":"+u.getAge()+",\"sex\":"+u.getSex()+"},";
builder.append(result);
}
//get count
results=builder.toString();
results=results.substring(0, results.length()-1)+"]";
results=results+",\"total\":"+count+"}";
}else {
results="{\"users\":[],"+"\"total\":"+count+"}";
}
System.out.println(results);
try {
//set cross domain
resp.setHeader("Access-Control-Allow-Origin", "*");
resp.getWriter().write(results);
} catch (IOException e) {
e.printStackTrace();
}
}
}
前端的話找個(gè)分頁(yè)插件就ok了,效果圖為
代碼地址:
鏈接:https://pan.baidu.com/s/18ypR3uEwvGImCmDngn0ugA
提取碼:ilul
總結(jié)
以上是生活随笔為你收集整理的java web 分页技术_javaweb分页的后端实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 接收list参数_what?你竟然不知道
- 下一篇: 服务器系统架构的评估,系统架构师:性能评