pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>untitled1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>untitled1 Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>untitled1</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
创建配置⽂件 application.yml
server: port: 8181 spring: mvc: view: prefix: / suffix: .jsp
创建Handler
package com.yin.controller; import com.yin.entity.Student; import com.yin.repository.StudentRepositroy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/hello") public class HelloHandler { @Autowired private StudentRepositroy studentRepositroy; @GetMapping("/index") public ModelAndView index(){ ModelAndView modelAndView =new ModelAndView(); modelAndView.setViewName("index"); modelAndView.addObject("list",studentRepositroy.findAll()); return modelAndView; } @GetMapping("/deleteById/{id}") public String deleteById(@PathVariable("id") long id){ studentRepositroy.deleteById(id); return "redirect:/hello/index"; } @PostMapping("/save") public String save(Student student){ studentRepositroy.saveOrUpdate(student); return "redirect:/hello/index"; } @PostMapping("/update") public String update(Student student){ studentRepositroy.saveOrUpdate(student); return "redirect:/hello/index"; } @GetMapping("/findById/{id}") public ModelAndView findById(@PathVariable("id") long id){ ModelAndView modelAndView= new ModelAndView(); modelAndView .setViewName("update"); modelAndView.addObject("student",studentRepositroy.findById(id)); return modelAndView; } }
JSP
<%-- Created by IntelliJ IDEA. User: Destiny Date: 2020/6/9 Time: 17:02 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <body> <h1>学生信息</h1> <table> <tr> <td>学生编号</td> <td>学生姓名</td> <td>学生年龄</td> <td>操作</td> </tr> <c:forEach items="${list}" var="student"> <tr> <td>${student.id}</td> <td>${student.name}</td> <td>${student.age}</td> <td> <a href="/hello/findById/${student.id}">xiugai</a> <a href ="/hello/deleteById/${student.id}">shachu</a> </td> </tr> </c:forEach> </table> <a href="/save.jsp">添加学生</a> </body> </html>
<%--
Created by IntelliJ IDEA.
User: Destiny
Date: 2020/6/9
Time: 17:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/hello/update" method="post">
ID:<input type="text" name="id" value="${student.id}" readonly/><br/>
name:<input type="text " name="name" value="${student.name}"/><br/>
age:<input type="text" name="age" value="${student.age}"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: Destiny
Date: 2020/6/9
Time: 17:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/hello/save" method="post">
ID:<input type="text" name="id"/><br/>
name:<input type="text " name="name"/><br/>
age:<input type="text" name="age"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>