Learn from the station b springcloud, now summarize the summary removal of a small error appearing in the video, some of the error-prone places were reminded
b outbound links: https://www.bilibili.com/video/av55993157
data link:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
extraction code: 21ru
On a link
next section link:
The following list summarizes:
New construction → parent pom → resources / application and java → com / southwind / controller / HelloHandler → startup class Application
Copy entity / Student and modify HelloHandler → repository → delete webapp / index.jsp The index.jsp add new code inside → Restart Application
→ Add index.jsp add the code controller / HelloHandler restart Application Code →
New save.jsp and the webapp → update.jsp codes applied in controller / HelloHandler → detecting codes added
Implementation details:
1. Create a new project springjsp
tick, select webapp, next
behind has been next, and finally Finish
wait for the download, if you first create should have to wait long!
Half an hour later update is completed, the bottom has a finish
2. Parent pom
deleted portion dependencies, 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>
The first to join the code above, there may be some red can not be resolved
to wait refreshes the red part is like, you can identify the
parent pom file the complete code:
<?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. In the main packet resources in the new folder
and mark the resource bundle
tie new folder java, and mark
the new configuration file in the file resources in application.yml
fill the code:
server:
port: 8181
spring: #视图解析器
mvc:
view:
prefix: / #前缀
suffix: .jsp #后缀
4. The package record folder in the java com.southwind.controller
new class HelloHandler.java control packet in the controller
tagging:
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 可以解析return 的 jsp和 html 页面
@RestController就不能 ,但 @RestController = @ResponseBody + @Controller
* */
@RequestMapping("/hello")
public class HelloHandler {
@GetMapping("/index")
public String index(){
System.out.println("index...");
return "index";
}
}
5. Create a new package in the Application startup class southwind the
tagging:
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
enter http: // localhost: 8181 / hello / index
this from hello world:
and index ... printed out in the console
7. entity and the packet on a packet in the repository copy of the project to package southwind
8. content of HelloHandler:
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 可以解析return 的 jsp和 html 页面
@RestController就不能 ,但 @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. The package of index.jsp webapp delete
new jsp file named index
9.1
, line 8 <% @ page contentType = " text / html; charset = UTF-8" language = "java"%> added later:
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
9.2 to add code <body> </ body> of:
<h1>学生信息</h1>
<table>
<tr>
<th>学生编号</th>
<th>学生姓名</th>
<th>学生年龄</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>学生信息</h1>
<table>
<tr>
<th>学生编号</th>
<th>学生姓名</th>
<th>学生年龄</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 startup class Application
enter HTTP: // localhost: 8181 / the Hello / index
11. modify, delete operations complement complete:
11.1 in index.jsp
<th>学生编号</th>
<th>学生姓名</th>
<th>学生年龄</th>
<th>操作</th>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>
<a href="/hello/findById/${student.id}">修改</a>
<a href="/hello/deleteById/${student.id}">删除</a>
</td>
In the <table> </ table> was added later (note the back!) /
Save slash indicates the start in front of the take root directory
<a href="/save.jsp">添加学生</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>学生信息</h1>
<table>
<tr>
<th>学生编号</th>
<th>学生姓名</th>
<th>学生年龄</th>
<th>操作</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}">修改</a>
<a href="/hello/deleteById/${student.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<a href="/save.jsp">添加学生</a>
</body>
</html>
11.2 Add the code in the controller / HelloHandler in:
Note @GetMapping delete a part of me did not wrong!
@GetMapping("/deleteById/{id}")
public String deleteById(@PathVariable("id")long id){
studentRepository.deleteById(id);
return "redirect:/hello/index";/* redirect:重对象 */
}
Test: Restart Application
enter http: // localhost: 8181 / hello / index
click Delete:
11.3 save.jsp new folder in the webapp
add code in <body> </ body> in which:
<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>
11.4 webapp new folder in update.jsp
add code <body> </ body> of:
<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="提交"/>
11.5 Add the code in the controller / HelloHandler in:
which update the corresponding @PostMapping ( "/ update") I did wrong
modelAndView.setViewName ( "update"); / * this update is to jump to the webapp / update.jsp in * /
@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");/*这个update就是跳转至webapp/update.jsp中*/
modelAndView.addObject("student",studentRepository.findById(id));
return modelAndView;
}
12 check
into the HTTP: // localhost: 8181 / the Hello / index
12.1 click Add student
submission:
12.2 modify the information
to modify age, submitted
12.3 deleted