In Build Java environment and output hello world, I tried to write and output hello world to the console. This time, let's go a little further and display HTML in the spring project.
If you haven't added spring yet, please refer to the previous articles. Add spring boot and gradle to Eclipse Install the plugin in Eclipse
First, let's create a spring project.
Decide on a name for your project or package. This time I chose helloSpring.
Press finish to complete. It was made like this.
It is about the dependency described in 4. To be honest, I'm not sure, but it seems that you can choose the Spring framework you want to use here. Is it an image of choosing what to ask Gradle for this? You can add it later by playing with the Gradle configuration file. (I would appreciate it if you could tell me if the recognition is different)
Let's take a look inside the created project.
Gradle's configuration file is build.gradle
, so open it.
buid.gradle
plugins {
id 'org.springframework.boot' version '2.3.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
/*Has been added ↓*/
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-web-services'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
/*Added ↑*/
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
The four you selected when selecting the dependency have been added. I don't have enough knowledge about this area, so I would like to summarize it someday.
Now that the project is complete, let's output it. Add two files. After adding it, it looks like this.
Basically put the HTML file in templates
in src / main / resources
.
Right-click templates => [New] => [Other] to create an HTML file, hello.
hello.html
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>hello</title>
</head>
<body>
<h1>Hello World!!</h1>
</body>
</html>
Add a package called helloSpring.Controller
and create HelloController.java
in it.
HelloController.java
package helloSpring.Controller;
import org.springframework.stereotype.Controller;
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 hello() {
return "hello";
}
}
What you should pay attention to here is the place to create the Controller.
If you do not place it in the same package as the startup class where the project's main method HelloSpringApplication.java
is located or under it, it will not be loaded correctly and a 404 error will occur.
You can create it in the helloSpring
package, or be careful of the package name when adding the package for Controller as introduced this time.
There is MVC, which is one of the concepts for organizing application settings that are generally adopted in web frameworks, and it is the C part. I think it's a little messy, but I'll summarize the details later, so I'll explain it briefly now.
You can't display it on the screen just by creating HTML, so you need a place to display it. Controller is about to write the content that I want you to do this processing when this URL comes.
First of all, those with @ are called annotations. Annotation means annotation.
By writing @Controller, this class is declared to be Controller. In @RequestMapping, write the process of what kind of request comes.
import is written to use these.
To explain this code in words from above (from @Controller), This is the HelloController class of Controller. When you receive a GET request with the URL "/", execute the function called hello. is what it means. The hello function describes the process of returning a file called hello.
When I run it, I see something like this on the console.
Since the port number is 8080, try accessing http: // localhost: 8080.
I was able to output safely.
Next, I would like to explain MVC and dynamically change the output characters. Next => Creating
Recommended Posts