[JAVA] Try to display hello world with spring + gradle

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

Let's make a spring project

First, let's create a spring project.

    1. Select [file] => [new] => [Other] => [Spring Starter Project]
  1. Decide on a name for your project or package. This time I chose helloSpring. image.png

    1. Project dependency selection. I chose four this time. image.png
  2. Press finish to complete. It was made like this. image.png

What is a dependency?

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.

Output hello world

Now that the project is complete, let's output it. Add two files. After adding it, it looks like this. image.png

1. 1. HTML added

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>

2. Controller added

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.

What is Controller?

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.

Take a look at the contents of the code

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.

Execute

When I run it, I see something like this on the console. image.png

Since the port number is 8080, try accessing http: // localhost: 8080. image.png

I was able to output safely.

Next, I would like to explain MVC and dynamically change the output characters. Next => Creating

Recommended Posts

Try to display hello world with spring + gradle
(IntelliJ + gradle) Hello World with Spring Boot
Easy to display hello world with Rails + Docker
Hello World with Spring Boot
Hello World with Spring Boot!
Hello World with Spring Boot
Hello World with SpringBoot / Gradle
Until "Hello World" with Spring Boot
(Intellij) Hello World with Spring Boot
Hello World with Eclipse + Spring Boot + Maven
[Practice! ] Display Hello World in Spring Boot
How Spring Security works with Hello World
Hello world! With Spring Boot (Marven + text editor)
Hello World at explosive speed with Spring Initializr! !! !!
Try to implement login function with Spring Boot
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Try to automate migration with Spring Boot Flyway
I wanted to gradle spring boot with multi-project
Hello World with Micronaut
Hello World (REST API) with Apache Camel + Spring Boot 2
Try to work with Keycloak using Spring Security SAML (Spring 5)
Hello World (console app) with Apache Camel + Spring Boot 2
Hello World with VS Code!
Hello, World! With Asakusa Framework!
Create a Hello World web app with Spring framework + Jetty
Spring Boot Hello World in Eclipse
To display multiple lines with UILabel
Hello world with Kotlin and JavaFX
Hello World with Docker and C
Try to imitate marshmallows with MiniMagick
Hello World with GlassFish 5.1 + Servlet + JSP
Create PDF with itext7 ~ Hello World ~
"Hello world" for ImageJ with Eclipse
Hello World with GWT 2.8.2 and Maven
Try "Introduction to Vert.x" on Gradle
Hello world in Java and Gradle
Spring Boot gradle build with Docker
Until you run Hello World of JavaFX with VS Code + Gradle
Try Hello World with the minimum configuration of Heroku Java spring-boot
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
I want to display images with REST Controller of Java and Spring!
Try using Spring Boot with VS Code
Spring Boot2 Web application development with Visual Studio Code Hello World creation
Try to implement login function with Spring-Boot
Java development with Codenvy: Hello World! Run
"Hello, World!" With Kotlin + CLI in 5 minutes
How to create a server executable JAR and WAR with Spring gradle
Introduction to Ratpack (3) --hello world detailed explanation
Hello world with Kotlin and Tornado FX
Add scripts to distributions created with gradle
"Teacher, I want to implement a login function in Spring" ① Hello World
Add spring boot and gradle to eclipse
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
Spring Integration Study memorandum ~ Understanding Spring Integration Sample 1. Hello World ~
Create a website with Spring Boot + Gradle (jdk1.8.x)
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
Make HTML page dynamic with thymeleaf (spring + gradle)
Hello World with JavaFX 11 (OpenJFX) in Liberica JDK 11
Run Scala applications with Spring Boot through Gradle
How to use built-in h2db with spring boot
You use context to use MDC with Spring WebFlux