三、controller层代码编写
首先建一个名为web的包,随后建立一个StudentController。
StudentController
@Controller @RequestMapping("/student") public class StudentController { @Autowired private StudentBiz stubiz; // 增 @RequestMapping("/add") public String add(Student student){ stubiz.insertSelective(student); return "redirect:list"; } // 删 @RequestMapping("/del") public String del(Student student){ stubiz.deleteByPrimaryKey(student.getSid()); return "redirect:list"; } // 查 @RequestMapping("/list") public String list(Student student, HttpServletRequest request){ PageBean pageBean=new PageBean(); pageBean.setRequest(request); List<Student> students = stubiz.selectBySnamePager(student, pageBean); request.setAttribute("slist",students); request.setAttribute("pageBean",pageBean); return "student/list"; } // 改 @RequestMapping("/edit") public String edit(Student student){ stubiz.updateByPrimaryKeySelective(student); return "redirect:list"; } // 模糊分页查询 @RequestMapping("/PreSave") public String PreSave(Student student, HttpServletRequest request){ if(student!=null && student.getSid()!=null){ Student s = stubiz.selectByPrimaryKey(student.getSid()); request.setAttribute("s",s); } return "student/edit"; } }
查询这用的是绝对路径,所以还要加上student。
四、前台代码与分页代码
这里我需要用到分页,所以将以前我所编写的自定义分页标签加入。
首先建立一个名为tag的包,将PageTag.java导入
①PageTag.java
package com.csdn.xw.tag; import com.csdn.xw.util.PageBean; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyTagSupport; import java.io.IOException; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class PageTag extends BodyTagSupport{ private PageBean pageBean;// 包含了所有分页相关的元素 public PageBean getPageBean() { return pageBean; } public void setPageBean(PageBean pageBean) { this.pageBean = pageBean; } @Override public int doStartTag() throws JspException { // 没有标签体,要输出内容 JspWriter out = pageContext.getOut(); try { out.print(toHTML()); } catch (IOException e) { e.printStackTrace(); } return super.doStartTag(); } private String toHTML() { StringBuffer sb = new StringBuffer(); // 隐藏的form表单---这个就是上一次请求下次重新发的奥义所在 // 上一次请求的URL sb.append("<form action='"+pageBean.getUrl()+"' id='pageBeanForm' method='post'>"); sb.append(" <input type='hidden' name='page'>"); // 上一次请求的参数 Map<String, String[]> paramMap = pageBean.getParamMap(); if(paramMap != null && paramMap.size() > 0) { Set<Entry<String, String[]>> entrySet = paramMap.entrySet(); for (Entry<String, String[]> entry : entrySet) { // 参数名 String key = entry.getKey(); // 参数值 for (String value : entry.getValue()) { // 上一次请求的参数,再一次组装成了新的Form表单 // 注意:page参数每次都会提交,我们需要避免 if(!"page".equals(key)) { sb.append(" <input type='hidden' name='"+key+"' value='"+value+"' >"); } } } } sb.append("</form>"); // 分页条 sb.append("<ul class='pagination justify-content-center'>"); sb.append(" <li class='page-item "+(pageBean.getPage() == 1 ? "disabled" : "")+"'><a class='page-link'"); sb.append(" href='javascript:gotoPage(1)'>首页</a></li>"); sb.append(" <li class='page-item "+(pageBean.getPage() == 1 ? "disabled" : "")+"'><a class='page-link'"); sb.append(" href='javascript:gotoPage("+pageBean.previousPage()+")'><</a></li>");// less than 小于号 // sb.append(" <li class='page-item'><a class='page-link' href='#'>1</a></li>"); // sb.append(" <li class='page-item'><a class='page-link' href='#'>2</a></li>"); sb.append(" <li class='page-item active'><a class='page-link' href='#'>"+pageBean.getPage()+"</a></li>"); sb.append(" <li class='page-item "+(pageBean.getPage() == pageBean.maxPage() ? "disabled" : "")+"'><a class='page-link' href='javascript:gotoPage("+pageBean.nextPage()+")'>></a></li>"); sb.append(" <li class='page-item "+(pageBean.getPage() == pageBean.maxPage() ? "disabled" : "")+"'><a class='page-link' href='javascript:gotoPage("+pageBean.maxPage()+")'>尾页</a></li>"); sb.append(" <li class='page-item go-input'><b>到第</b><input class='page-link'"); sb.append(" type='text' id='skipPage' name='' /><b>页</b></li>"); sb.append(" <li class='page-item go'><a class='page-link'"); sb.append(" href='javascript:skipPage()'>确定</a></li>"); sb.append(" <li class='page-item'><b>共"+pageBean.getTotal()+"条</b></li>"); sb.append("</ul>"); // 分页执行的JS代码 sb.append("<script type='text/javascript'>"); sb.append(" function gotoPage(page) {"); sb.append(" document.getElementById('pageBeanForm').page.value = page;"); sb.append(" document.getElementById('pageBeanForm').submit();"); sb.append(" }"); sb.append(" function skipPage() {"); sb.append(" var page = document.getElementById('skipPage').value;"); sb.append(" if (!page || isNaN(page) || parseInt(page) < 1 || parseInt(page) > "+pageBean.maxPage()+") {"); sb.append(" alert('请输入1~"+pageBean.maxPage()+"的数字');"); sb.append(" return;"); sb.append(" }"); sb.append(" gotoPage(page);"); sb.append(" }"); sb.append("</script>"); return sb.toString(); } }
将tld文件导入WEB-INF下
②zking.tld
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>zking 1.1 core library</description> <display-name>zking core</display-name> <tlib-version>1.1</tlib-version> <short-name>zking</short-name> <uri>http://jsp.veryedu.cn</uri> <tag> <name>page</name> <tag-class>com.csdn.xw.tag.PageTag</tag-class> <body-content>JSP</body-content> <attribute> <name>pageBean</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
③list.jsp
<%-- Created by IntelliJ IDEA. User: 索隆 Date: 2023/9/8 Time: 16:32 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css" rel="stylesheet"> <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js"></script> <title>学生管理首页</title> <style type="text/css"> .page-item input { padding: 0; width: 40px; height: 100%; text-align: center; margin: 0 6px; } .page-item input, .page-item b { line-height: 38px; float: left; font-weight: 400; } .page-item.go-input { margin: 0 10px; } </style> </head> <body> <form class="form-inline" action="${pageContext.request.contextPath }/student/list" method="post"> <div class="form-group mb-2"> <input type="text" class="form-control-plaintext" name="sname" placeholder="请输入学生名称"> <!-- <input name="rows" value="20" type="hidden"> --> <!-- 不想分页 --> <%--<input name="pagination" value="false" type="hidden">--%> </div> <button type="submit" class="btn btn-primary mb-2">查询</button> <a class="btn btn-primary mb-2" href="${pageContext.request.contextPath }/student/PreSave">新增</a> </form> <table class="table table-striped "> <thead> <tr> <th scope="col">学生ID</th> <th scope="col">学生名称</th> <th scope="col">学生年龄</th> <th scope="col">学生性别</th> <th scope="col">操作</th> </tr> </thead> <tbody> <c:forEach var="s" items="${slist }"> <tr> <td>${s.sid }</td> <td>${s.sname }</td> <td>${s.sage }</td> <td>${s.ssex }</td> <td> <a href="${pageContext.request.contextPath }/student/PreSave?sid=${s.sid}">修改</a> <a href="${pageContext.request.contextPath }/student/del?sid=${s.sid}">删除</a> </td> </tr> </c:forEach> </tbody> </table> <!-- 这一行代码就相当于前面分页需求前端的几十行了 --> <z:page pageBean="${pageBean }"></z:page> ${pageBean } ${slist} </body> </html>
④edit.jsp
<%-- Created by IntelliJ IDEA. User: 索隆 Date: 2023/9/8 Time: 16:49 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>编辑界面</title> </head> <body> <center> 编辑界面 <form action="${pageContext.request.contextPath }/${empty s ? 'student/add' : 'student/edit'}" method="post"> sid:<input type="text" name="sid" value="${s.sid }"><br> sname:<input type="text" name="sname" value="${s.sname }"><br> sage:<input type="text" name="sage" value="${s.sage }"><br> ssex:<input type="text" name="ssex" value="${s.ssex }"><br> <input type="submit"> </form> </center> </body> </html>
五、案例测试
5.1.模糊查询
5.2.新增
5.3.修改
5.4.删除
5.5.分页
到这里我的分享就结束了,欢迎到评论区探讨交流!!
💖如果觉得有用的话还请点个赞吧 💖