时间:2021-07-01 10:21:17 帮助过:16人阅读
2service层
package com.hanqi.service; import java.util.List; import com.alibaba.fastjson.JSONArray; import com.han.entity.Student; import com.hanqi.dao.StudentDAO; public class StudentService { //查询分页数据,并返回JSON public String getPageJSON(int page, int rows) { String rtn = "{‘total‘:0,‘rows‘:[ ]}" ; int total = new StudentDAO().getTotal() ; if(total > 0) { List<Student> list = new StudentDAO().getPageList(page, rows) ; //将List集合转为JSON集合 String json_list = JSONArray.toJSONString(list) ; //转义字符返回复合类型的JSON字符串 rtn = "{\"total\":"+total+",\"rows\":"+json_list+"}" ; } return rtn ; } }
3servlet
package com.hanqi.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hanqi.service.StudentService; /** * Servlet implementation class StudentServlet */ public class StudentServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public StudentServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); //接收请求 //1每页行数 String rows = request.getParameter("rows") ; //2页码 String page = request.getParameter("page") ; //System.out.println("rows = "+ rows ); //System.out.println("page = " + page ); if(page != null && rows != null) { int rowss = Integer.parseInt(rows) ; int pagess = Integer.parseInt(page) ; String json_list = new StudentService().getPageJSON(pagess, rowss) ; //返回数据 System.out.println(json_list); response.getWriter().write(json_list) ; } else { response.getWriter().write("{total:0,rows:[]}") ; } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
4html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <!-- 1 jQuery的js包 --> <script type="text/javascript" src="jquery-easyui-1.4.4/jquery.min.js"></script> <!-- 2 css资源 --> <link rel="stylesheet" type="text/css" href="jquery-easyui-1.4.4/themes/default/easyui.css"> <!-- 3 图标资源 --> <link rel="stylesheet" type="text/css" href="jquery-easyui-1.4.4/themes/icon.css"> <!-- 4 EasyUI的js包 --> <script type="text/javascript" src="jquery-easyui-1.4.4/jquery.easyui.min.js"></script> <!-- 5 本地语言 --> <script type="text/javascript" src="jquery-easyui-1.4.4/locale/easyui-lang-zh_CN.js"></script> </head> <body> <script type="text/javascript"> $(function(){ $(‘#dg‘).datagrid({ url:‘StudentServlet‘, columns:[[ {field:‘sno‘,title:‘课程号‘,width:100}, {field:‘sname‘,title:‘姓名‘,width:100}, {field:‘ssex‘,title:‘性别‘,width:100,align:‘right‘}, {field:‘sbirthday‘,title:‘生日‘,width:100,align:‘right‘}, {field:‘sclass‘,title:‘班级‘,width:100,align:‘right‘} ]], pagination:true,//分页 fitColumns:true,//列自适应宽度 }); }) </script> <table id="dg"></table> </body> </html>
运用EasyUI中datagrid读取数据库数据实现分页
标签:image 适应 nal row link except package sse cti