[Java] Let’s create a TODO application in Java 2 I want to make a Hello World by creating a template with Spring Initializr

2 minute read

Hello. Let’s create a TODO application in Java 1 Brief explanation of MVC is the following article.

For the time being, I’d like to create a Controller and display a View from there.

1: Brief description of MVC 2: I want to make a template with Spring Initializr and do Hello world (now here)

Create Spring template

First, let’s make a Spring template with Spring Initializr.

Screen Shot 0002-06-01 at 12.53.18.png

I will proceed simply like this.

I will add JPA and lombok in the future, but I will add them at the same time as updating, so this time only this configuration is OK!

Preparation for Build and Run

Build

Screen Shot 0002-06-01 at 14.39.14.png

When you move the cursor to the top of Intel iJ, the menu bar will appear, so press Build -> Build Project to build it. It may take a while, but let’s wait. You should see something like a build in progress at the bottom of Intel iJ.

Prepare to run with Edit Configurations.

↓ Press Run -> Edit Configurations from the menu bar at the top of Intel iJ. Screen Shot 0002-06-01 at 14.39.37.png

↓ Let’s enter the highlighted part like this. For JRE, select Java 11 series. Screen_Shot_0002-06-01_at_14_40_01.png

TodoController creation

Next is the creation of the controller.

Please make a controller according to the following path.

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller // ①
public class ToDoController {

    @GetMapping("/top") // ②
    public String top(){
        return "top"; //③
    }
}

① Controller declaration

You can declare that this class is a controller by using @Controller. The description @@~~~ is called an annotation. (I feel like I can call various useful things.)

② Get Mapping

@GetMapping annotation will process the method immediately below when a request comes in by the Get Method of Http Method to the address in the argument.

③ Return top.html

It is as it is. You can call the html file under templates by writing this (extension is omitted) The return value of the class when displaying html is String.

About import

I think that import will be added automatically when annotation is written, but if it is not done, you can also add it by Option + Enter while hovering the annotation.

Write 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 output Hello world simply like this. The HTML files under templates can be called with the above controller syntax! !!

Actually run and check

How to run

Screen Shot 0002-06-01 at 14.57.30.png

I think there is a green arrow in the upper right corner of Intel iJ, so click it and run the project!

Then

Screen_Shot_0002-06-01_at_15_00_37.png

You should see a window like this at the bottom of your screen.

If it is Started, it has started successfully. If there is an error, let’s google it! (I’m in trouble because there aren’t many.)

access now

If you visit localhost:8080/top, you will see hello world!

Next time I will write the processing of the registration part.