We are planning to develop with SpringBoot + Thymeleaf + Gradle at the next site, For both rabbits and horns, start with Hello World!
So, at first, I will simply use StringBoot and describe the procedure to display Hello World in HTML.
OS:windows10 Pro JDK:11.0.3 IDE: STS (Spring Tool Suite) 3.9.8.RELEASE * Hereinafter referred to as STS. Build tool: Gradle 3.X
First, create a project named "Hello-sample" by following the steps below.
** 1-1. ** Select [File (or right-click on the project folder)]> [New]> [Spring Starter Project] from the STS menu.
** 1-2. ** On the screen below, enter "hello-sample" as the name, select "Gradle (Buildship 3.x)" as the type, select the Java version to use, and click the "Next" button.
** 1-3. ** On the screen below, select "Web" and "Thymeleaf" and click the "Finish" button.
When you create a new project, you should see the following structure.
** 1-4. ** Right-click on the project and select Run> Spring Boot Application.
The following log is output to the STS console. If there are no errors, the "Spring Starter Project" has been created successfully.
Create the screen to be displayed next, that is, HTML.
** 2-1. ** Select File (or right-click on the project folder)> New> Other from the STS menu.
** 2-2. ** On the displayed screen, enter "html" in the wizard field and apply a filter. "HTML file" will be displayed. Select this and click the "Next" button.
** 2-3. ** Specify the location and file name to create the HTML file, and click the "Finish" button. -Specify the location to create: Enter the following path in [Enter or select parent folder]. hello-sample/src/main/resources/templates -Specify file name: Enter "index.html" in [File name].
** 2-4. ** Change the created HTML file so that it has the following contents.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
<meta charset="utf-8" />
</head>
<body>
<h1>Hello SpringBoot Sample</h1>
</body>
</html>
I have prepared the HTML to display, but it cannot be displayed as it is. Create a Controller class that will be "C" in MVC.
** 3-1. ** From the STS menu, select [File (or right-click on the project folder)]> [New]> [Class] and create a class with the name "HelloController".
I think it will be configured like this.
** 3-2. ** Change the created Controller class so that it has the following contents.
HelloController.java
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HelloController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Model model) {
return "index"; //Specify the name of the HTML file to be displayed (no extension required)
}
}
Once this is done, right-click on the project> Run> Spring Boot Application to run it. After starting, try hitting the following URL from your browser. URL:http://localhost:8080/
The following screen will be displayed.
that's all. In the next article, I'd like to pass a value from the Controller class.
Recommended Posts