Hello. This is a continuation of Let's create a TODO application in Java 1 Brief explanation of MVC.
This time, I would like to create a Controller and display the View from there.
1: Brief description of MVC 2: I want to make a template with Spring Initializr and make a Hello world (here and now)
First, let's create a Spring template with Spring Initializr.
I will simply proceed like this.
We will add JPA and lombok in the future, but we will add them at the same time as we update, so this time only this configuration is OK!
When you move the cursor to the top of Intel iJ, the menu bar will appear. Press ** Build-> Build Project ** to build. It may take some time, but let's wait. At the bottom of Intel iJ, you should see something like building.
↓ Press ** Run-> Edit Configurations ** from the menu bar at the top of Intel iJ.
↓ Let's enter the highlighted part like this. Select Java 11 series for JRE.
Next is the creation of the controller.
Make a controller according to the path below.
java:todo>src>main>java>com.example.todo>TodoController
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller //①
public class ToDoController {
@GetMapping("/top") //②
public String top(){
return "top"; //③
}
}
You can declare this class to be a controller by doing @Controller. The description @ ~~~ is called an annotation. (It feels like a person who can call various useful things.)
②GetMapping The @GetMapping annotation processes the method directly below when a request comes to the address in the argument with Get Method of Http Method.
As it is. You can call the html file under templates by writing this (extension omitted) The return value of the class when displaying html is String.
I think that import will be added automatically when you write an annotation, but if it is not done, you can also add it by pressing ** Option + Enter ** while hovering the annotation.
Todo>src>resources>templates>top.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>hello world</title>
</head>
<body>
<p>hello world</p>
</body>
</html>
Let's simply output Hello world like this. The HTML file below templates can be called by writing the controller above! !!
I think there is a green arrow in the upper right corner of Intel iJ, so click on it to run the project!
Then
You should see a window like this at the bottom of the screen.
If it is Started, it has started successfully. If you get an error, try google with that sentence! (I'm in trouble because it doesn't come out quite a bit.)
If you visit [localhost: 8080 / top](http: // localhost: 8080 / top), you will see hello world!
Next time, I will write the processing of the registration part.
Recommended Posts