We will develop a web application for Spring Boot 2 with Visual Studio Code. This is the Spring Boot version of Hello World created earlier here.
Please build the development environment by referring to here. Maven and Tomcat are not required. Add the extension "Spring Boot Extension Pack".
OS:Windows 10 Pro 64bit Editor:Visual Studio Code 1.44.2 JDK:AdoptOpenJDK 11.0.6+10 x64
You can do it on Visual Studio Code, but you can also create it with spring initializr (https://start.spring.io/). This time I did it like this.
Expand the created template to "D: \ JAVA \ Project".
Create it in "D: \ JAVA \ Project \ bootSample1 \ src \ main \ java \ com \ example \ bootSample1 \ web \ controller". If the folder does not exist, create it.
RootController.java
RootController.java
package com.example.bootSample1.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class RootController {
@GetMapping("/")
public String root() {
// "redirect:"Prefix to redirect
return "redirect:hello/index";
}
}
HelloController.java
HelloController.java
package com.example.bootSample1.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloController {
@GetMapping("/index")
public String indexGet() {
return "hello/index";
}
}
Create it in "D: \ JAVA \ Project \ bootSample1 \ src \ main \ resources \ templates \ hello". If the folder does not exist, create it.
index.html
index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
Press the "F5" key to execute. http://localhost:8080/ Please access. Make sure you are automatically redirected to [http: // localhost: 8080 / hello / index](http: // localhost: 8080 / hello / index).
It is OK if the following page is displayed.
Controller and View are exactly the same as the previous non-Boot sample. The convenience of Boot is that it does not require any preparation.
Recommended Posts