1.对象的反射:通过对象找出该类中所有的方法
Class cla=this.getClass();
Method method=cla.getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class );
例子:通过参数能调用该类中参数所对应的方法
超链接请求,参数method对应的是要调用的方法,pageNo对应的是分页显示的要显示第几页
<h3><a href="${pageContext.request.contextPath}/FunctionServlet?method=getUserList&pageNo=1">
查询所有用户信息。</a></h3>反射处理:
1.首先建立一个父类BaseServlet
@WebServlet("/BaseServlet") public class BaseServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected static int pageNo=1;//设置默认显示的页码 protected static int pageSize = 1;//每页有多少条记录 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ pageNo=Integer.parseInt(request.getParameter("pageNo")); }catch(Exception e){ e.getMessage(); } //获取用户传递的请求参数 String methodName = request.getParameter("method"); //通过方法名获取到方法的对象 //获取当前类的Class对象 Class cla = this.getClass(); //获取cla的的方法(Method对象) //getDeclaredMethod需要两个参数,方法名和参数名 //因为在java需要通过方法名和参数列表来确定一个方法 try { //获取方法对象 Method method = cla.getDeclaredMethod(methodName, HttpServletRequest.class , HttpServletResponse.class); //设置方法的访问权限 method.setAccessible(true); //调用方法 //invoke用于调用一个方法,第一个参数时要调用方法的对象,剩下是调用方法需要的参数 method.invoke(this, request , response); } catch (Exception e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf8"); doGet(request, response); } }
2. 建立一个子类继承BaseServlet父类,定义每个参数对应的要调用的方法
@WebServlet("/FunctionServlet") public class FunctionServlet extends BaseServlet { private static final long serialVersionUID = 1L; private Userservice userservice=new UserserviceImp(); public void loginByNameAndPwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ String username=request.getParameter("username"); String password=request.getParameter("password"); request.setAttribute("username", username); int resule=userservice.findByNameAndPwd(username, password); if(resule==1){ response.sendRedirect(request.getContextPath()+"/login-success.jsp?username="+username+""); //request.getRequestDispatcher("/login-success.jsp").forward(request, response); }else{ response.sendRedirect(request.getContextPath()+"/login-error.html"); } } public void getUserList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ String url=WEBUtils.getPath(request); Page<User> page=userservice.getUserList(pageNo, pageSize); page.setPath(url); request.setAttribute("page", page); request.getRequestDispatcher("/WEB-INF/pages/list.jsp").forward(request, response); } }
以后在参数中写上要调用的方法名即可调用
二、信息分页处理
1.建立page实体类,用到泛型
public class Page<T> { private List<T> data; //后台数据库查询出来 private int totalRecord; //表示总共有多少记录,从数据库中查询出来 // private int totalPage; // 表示总共有多少页,计算得到! // private int index; //表示当前索引值,计算出来的! private int pageNumber; //表示的是当前页码,这个参数是从页面传递过来的! private int pageSize; //表示的是每页显示多少条数据 ,在servlet中设置的! private String path;//表示超链接地址URL public Page() { super(); } public Page(int totalRecord, int pageNumber, int pageSize) { super(); this.totalRecord = totalRecord; this.pageNumber = pageNumber; this.pageSize = pageSize; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public List<T> getData() { return data; } public void setData(List<T> data) { this.data = data; } public int getTotalRecord() { return totalRecord; } public void setTotalRecord(int totalRecord) { this.totalRecord = totalRecord; } public int getTotalPage() { /** * totalPage:表示总共有几页! * * 总页数 totalRecord[总记录数] pageSize * 5 10 2 * 5 9 2 * 4 8 2 */ if(totalRecord % pageSize ==0){ return totalRecord / pageSize; } return (totalRecord / pageSize + 1); } public int getIndex() { /** * index表示的是当前索引值,是计算得到的! * 当前索引值 每页显示几条数据 当前页是第几页 * 0 3 1 * 3 3 2 * index = (pageNumber -1)*pageSize; */ return (getPageNumber() -1)*pageSize; } public int getPageNumber() { if(pageNumber < 1){ return 1; }else if(pageNumber > getTotalPage()){ return getTotalPage(); } return pageNumber; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }
2.怎么获取URL?
String url=WEBUtils.getPath(request);WEBUtils类中的getPath方法
public static String getPath(HttpServletRequest request){ String requestURI = request.getRequestURI(); String queryString = request.getQueryString(); String url = requestURI+"?"+queryString; if(url.contains("&pageNo")){ url = url.substring(0, url.indexOf("&pageNo")); } return url; }
3.显示列表的Servlet中的方法
protected void getStuList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String url = WEBUtils.getPath(request); Page<Student> page=stuService.getStudentList(pageNo,pageSize); page.setPath(url); request.setAttribute("page",page ); request.getRequestDispatcher("/WEB-INF/view/list.jsp").forward(request, response); }
业务层Service中的函数
public Page<Student> getStudentList(int pageNo, int pageSize){ //第一步:查询当前表的所有记录数 int totalRecord= stuDao.getTotalRecord(); //第二步:创建page对象 Page<Student> page = new Page<Student>(totalRecord,pageNo,pageSize); //第三步:查询分页列表数据并设置到page对象中! List<Student> list = stuDao.getLimitStuList(page.getIndex(),page.getPageSize()); page.setData(list); return page; }
持有层Dao中要有1.获取所有记录的方法2.分页显示的方法
视图层页面显示代码:将页表显示分离到另一个页面,通过静态引入
总体显示页面 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" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <c:choose> <c:when test="${empty page.data }"> <h1 align="center">没有学生数据</h1> </c:when> <c:otherwise> <h1 align="center">学生列表</h1> <table border="1" align="center" width="50%"> <tr> <th>姓名</th> <th>密码</th> <th>邮箱</th> <th colspan="2">操作</th> </tr> <c:forEach items="${page.data}" var="stu"> <tr> <td>${stu.username }</td> <td>${stu.password }</td> <td>${stu.mail }</td> <td><a href="${pageContext.request.contextPath}/StudentServlet?method=toUpdatePage&id=${stu.id}" >修改</a></td> <td><a href="#?${stu.id}">删除</a></td> </tr> </c:forEach> <tr><td colspan="5" align="center"><a href="#">添加</a></td></tr> </table> <br/> <%@ include file="/WEB-INF/pages/paging.jsp" %> </c:otherwise> </c:choose> </body> </html>页表显示页面 paging.jsp 显示5页的数据
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <div id="page_nav" align="center"> <a href="${page.path}&pageNo=1">首页</a> <a href="${page.path}&pageNo=${page.pageNumber -1 }">上一页</a> <c:choose> <c:when test="${page.totalPage <= 5 }" > <c:set var="begin" value="1"></c:set> <c:set var="end" value="${page.totalPage}"></c:set> </c:when> <c:when test="${page.pageNumber <= 3 }"> <c:set var="begin" value="1"></c:set> <c:set var="end" value="5"></c:set> </c:when> <c:otherwise> <c:set var="begin" value="${page.pageNumber-2}"></c:set> <c:set var="end" value="${page.pageNumber+2}"></c:set> <c:if test="${end > page.totalPage }"> <c:set var="begin" value="${page.totalPage-4}"></c:set> <c:set var="end" value="${page.totalPage}"></c:set> </c:if> </c:otherwise> </c:choose> <c:forEach begin="${begin }" end="${end}" var="num"> <c:if test="${page.pageNumber == num }"> 【${num}】 </c:if> <c:if test="${page.pageNumber != num }"> <a href="${page.path}&pageNo=${num}">${num }</a> </c:if> </c:forEach> <a href="${page.path}&pageNo=${page.pageNumber +1}">下一页</a> <a href="${page.path}&pageNo=${page.totalPage}">末页</a> 共${page.totalPage }页,${page.totalRecord }条记录到,去第<input value="${page.totalPage }" name = "pn" id ="pn_input"/>页 <input type="button" value="确定" id="btn_id"/> <script type="text/javascript"> $("#btn_id").click(function(){ var value= $("#pn_input").val(); window.location="${page.path}&pageNo="+value; }); </script> </div>