Learn spring cloud from station b, and now make a summary. This summary removes the small errors in the video, and reminds some mistakes
b station link: https://www.bilibili.com/video/av55993157
Material link:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
Extraction code: 21ru
Next link:
The following is a summary:
Create project → parent pom file → entity class → interface and implementation → start class → detection → configuration file
Implementation details:
1. New project spring JSP
Check, select webapp, next
next, Finish
Waiting for download, if the first creation should wait a long time!
The update is finished in half an hour. There is a finish at the bottom
2. father pom
Delete the dependencies section and add code after properties:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> <dependencies> <!--web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--jsp--> <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> <!--JSTL--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> </dependencies>
Add the above code for the first time. Some parts may be red and cannot be parsed
Wait for the red part to be refreshed, and then you can recognize it
Full code of parent pom file:
<?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>com.southwind</groupId> <artifactId>springjsp</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>springjsp 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.0.3.RELEASE</version> </parent> <dependencies> <!--web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--jsp--> <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> <!--JSTL--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> </dependencies> <build> <finalName>springjsp</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>
3. Create a new folder resources in package main
And marked as resource package
Create a new folder java and mark
Create a new configuration file application.yml in the file resources
Fill in Code:
server: port: 8181 spring: #view resolver mvc: view: prefix: / #prefix suffix: .jsp #Suffix
4. Create the package com.southwind.controller in the folder java
Create a new control class HelloHandler.java in package controller
Add code:
package com.southwind.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller /* @Controller jsp and html pages that can parse return @RestController No, but @ RestController = @ResponseBody + @ Controller * */ @RequestMapping("/hello") public class HelloHandler { @GetMapping("/index") public String index(){ System.out.println("index..."); return "index"; } }
5. Create a new startup class Application in package southwind
Add code:
package com.southwind; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
6. start Application
Go to http://localhost:8181/hello/index
This hello world comes from:
And index... It's printed in the console
7. Copy the package entity and package repository in the previous section to the project package southwind
8. Modify HelloHandler:
The code becomes:
package com.southwind.controller; import com.southwind.repository.StudentRepository; 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.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller /* @Controller jsp and html pages that can parse return @RestController No, but @ RestController = @ResponseBody + @ Controller * */ @RequestMapping("/hello") public class HelloHandler { @Autowired private StudentRepository studentRepository; @GetMapping("/index") public ModelAndView index(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("index"); modelAndView.addObject("list",studentRepository.findAll()); return modelAndView; } }
9. Delete index.jsp in the package webapp
Create a new jsp file named index
9.1
Line 8 <% @ page contenttype = "text/html;charset=UTF-8" language = "java"% > followed by:
<%@ page isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
9.2 add code in < body > < body >:
<h1>Student information</h1> <table> <tr> <th>Student number</th> <th>Student name</th> <th>Student age</th> </tr> <c:forEach items="${list}" var="student"> <tr> <td>${student.id}</td> <td>${student.name}</td> <td>${student.age}</td> </tr> </c:forEach> </table>
So index.jsp complete code:
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2020-03-05 Time: 12:00 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>Student information</h1> <table> <tr> <th>Student number</th> <th>Student name</th> <th>Student age</th> </tr> <c:forEach items="${list}" var="student"> <tr> <td>${student.id}</td> <td>${student.name}</td> <td>${student.age}</td> </tr> </c:forEach> </table> </body> </html>
10. Restart Application
Go to http://localhost:8181/hello/index
11. The operation of modification and deletion is complete:
11.1 in index.jsp
< th > student number < / th > < th > student name < / th > < th > student age < / th > < th > operation < / th >
<td>${student.id}</td> <td>${student.name}</td> <td>${student.age}</td> <td> <a href="/hello/findById/${student.id}">modify</a> <a href="/hello/deleteById/${student.id}">delete</a> </td>
Add after < Table > < / Table >
/The slash in front of save indicates to start from the root directory
<a href="/save.jsp">Add student</a>
Complete index.jsp Code:
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2020-03-05 Time: 12:00 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>Student information</h1> <table> <tr> <th>Student number</th> <th>Student name</th> <th>Student age</th> <th>operation</th> </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}">modify</a> <a href="/hello/deleteById/${student.id}">delete</a> </td> </tr> </c:forEach> </table> <a href="/save.jsp">Add student</a> </body> </html>
11.2 add code to controller/HelloHandler:
Note that I did not write @ GetMapping incorrectly!
@GetMapping("/deleteById/{id}") public String deleteById(@PathVariable("id")long id){ studentRepository.deleteById(id); return "redirect:/hello/index";/* redirect: Heavy object */ }
Test it: restart Application
Go to http://localhost:8181/hello/index
After clicking delete:
11.3 create a new save.jsp in the folder webapp
Add code in < body > < 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="Submission"/> </form>
11.4 create a new update.jsp in the folder webapp
Add code in < body > < 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="Submission"/>
11.5 add code to controller/HelloHandler:
I wrote the @ PostMapping("/update") corresponding to update correctly
modelAndView.setViewName("update"); / * this update is to jump to webapp/update.jsp */
@PostMapping("/save") public String save(Student student){ studentRepository.saveorupdate(student); return "redirect:/hello/index"; } @PostMapping("/update") public String update(Student student){ studentRepository.saveorupdate(student); return "redirect:/hello/index"; } @GetMapping("/findById/{id}") public ModelAndView findById(@PathVariable("id")long id){ ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("update");/*This update is to jump to webapp/update.jsp*/ modelAndView.addObject("student",studentRepository.findById(id)); return modelAndView; }
12 check
Go to http://localhost:8181/hello/index
12.1 Click to add students
After submission:
12.2 modification information
Change age, submit
12.3 delete