[JAVA] How to display characters entered in Spring Boot on a browser and reference links [Introduction to Spring Boot / For beginners]

Assumptions and environment

I'm studying how to use the Java framework Spring Boot. This time with Spring Boot *** Output of entered characters *** Now that I can do it, I will write it as a memorandum for myself and for beginners.

There were a lot of terms that I had never seen before, such as variables and methods that appeared on the way, so I will explain it in a bit of a bit.

environment: ・ Windows 10 ・ Eclipse · Java 8 ・ Spring Boot 2.0.4 Release ・ Thymeleaf ・ Maven

Introducing Spring Boot

Install STS from Eclipse Help> Marketplace. This article is insanely easy to understand. [Web application development starting with Spring Tool Suite (STS) and Spring Boot (1)](https://www.techscore.com/blog/2016/11/22/start-with-sts-and-spring-boot- 1 /) If you proceed according to this article, it's OK. Dependencies are Thymeleaf, Maven, Web only. Follow this article to Hello, World !.

Display the entered characters on the browser

Follow the continuation of the article linked above. It has become difficult to understand from here, so I will explain it.

First, let's play with the files created during Hello, World !.

--Edit HelloController.java (not really good, but reuse it) --Edit index.html

*** Basic notation explanation ***

Editing Controller.java

HelloController.java


package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

//It's a Controller! Annotation
@Controller
public class HelloController {
    //RequestMapping "/"→ Can be called when accessing the root URL
    //→ Method can be specified for each access address!
    @RequestMapping(value="/",method=RequestMethod.GET)
    //A convenient one that can put both Model and View (can be a return value)
    public ModelAndView index(ModelAndView mav) {
        //View name settings
        mav.setViewName("index");
        //Set the value (msg) and the output character for it
        mav.addObject("msg", "Please enter your name:");
        return mav;
    }

    @RequestMapping(value="/",method=RequestMethod.POST)
    //"name"Get the parameter POSTed in and take it as an argument
    public ModelAndView send(@RequestParam("name")String name,ModelAndView mav) {
        mav.setViewName("index");
        mav.addObject("msg", "Hello,"+name+"!");
        //Enter name (input value) in value
        mav.addObject("value",name);
        return mav;
    }
}

The above is the source code of the controller. (If you're looking for a controller, search for "Java MVC"!) Since it is a Java class of the main source, create it in src / main / java.

I usually write it as a comment, but the terms I don't understand are as follows.

What is the difference between @Controller and @RestController?

This article will be helpful. Various return values in Spring MVC controller

In short @Controller is mainly used for controllers for web pages, @RestController is used separately It seems that. My head hasn't caught up yet, but I somehow understood what I wanted to say, so next.

What is ModelAndView?

ModelAndView is a function of Spring MVC, and is a class that can handle "View" (object to be displayed on the screen) and "Model" (object to exchange values with view) together. By putting ModelAndView in the argument, you can use the information collectively. Super convenient.

What is setViewName?

setViewName is "the one that sets the view name". By setting the view name to " index " in setViewName, the view named index will be used. So what is a view name? It seems that there are various things, so I will investigate it next time. Omitted.

Since the Thymeleaf template engine is used here, the set " index " is recognized as a Thymeleaf template (templates / index.html). View (screen display part) is generated based on the information of this view name.

So, put this " ViewName " and the output characters named " msg " together into the ModelAndView class mav. This is the good point of ModelAndView. So, if you return the mav, you can send the ViewName and msg together to the client side.

What is Thymeleaf?

A template engine such as HTML.

A library for processing with a program based on an HTML template called a template and outputting it to the screen. For the purpose of division of labor between programmers and web designers, processing is performed by interpreting and executing special tags. Smarty is a typical example in PHP. [From Weblio Dictionary](https://www.weblio.jp/content/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3 % 83% 88% E3% 82% A8% E3% 83% B3% E3% 82% B8% E3% 83% B3)

So, the following is easy to understand about the features of Thymeleaf. What is Thymeleaf In short, unlike JSP that is displayed after processing, it is described by attribute value, so it can be displayed normally without processing & it seems to be easy to see after all.

Edit index.html

Now that you understand it so far (even in Zackli), edit index.html.

index.html


<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Hello Page</title>
</head>
    <body>
        <!-- th:text is specified as text in Thymeleaf,${}Is a variable-->
        <p th:text="${msg}">please wait...</p>
        <form action="/" method="post">
        <input type="text" name="name" th:value="${value}" >
        <input type="submit" value="Click">
        </form>
    </body>
</html>

Put the HTML in the template file. src/main/resources>templates

This HTML file is written in Thymeleaf notation, isn't it? These two articles are easy to understand about Thymeleaf notation. Completely master Thymeleaf with the minimum required sample Thymeleaf standard syntax

With this as a reference, I wrote and copied it.

In this state, run the project as a Spring Boot application.

Result screen

SpringBoot入力文字出力.PNG

I was able to pass the value safely!

Recommended Posts

How to display characters entered in Spring Boot on a browser and reference links [Introduction to Spring Boot / For beginners]
[Spring Boot] How to create a project (for beginners)
Book introduction: Spring Boot Recommended reference book for beginners!
How to add a classpath in Spring Boot
How to write a unit test for Spring Boot 2
How to create a Spring Boot project in IntelliJ
How to display a browser preview in VS Code
How to call and use API in Java (Spring Boot)
How to display a graph in Ruby on Rails (LazyHighChart)
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
How to make CsrfRequestDataValueProcessor and original RequestDataValueProcessor coexist on Spring Boot
[For beginners] How to debug in Eclipse
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Java] [For beginners] How to insert elements directly in a 2D array
How to display a web page in Java
Sign in to a Spring Boot web application on the Microsoft ID platform
[Spring Boot] How to get properties dynamically from a string contained in a URL
How to bind to property file in Spring Boot
From building an AWS cloud environment to deploying a Spring Boot app (for beginners)
Until you create a Spring Boot project in Intellij and push it to Github
How to implement authentication process by specifying user name and password in Spring Boot
[Personal application work memo] How to display a bar graph and a line graph in one graph
[Swift] How to display the entered characters in Widget via UserDefaults when using WidgetKit
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
Plans to support JDK 11 for Eclipse and Spring Boot
How to set Dependency Injection (DI) for Spring Boot
[For beginners] DI ~ The basics of DI and DI in Spring ~
[For beginners] Minimum sample to display RecyclerView in Java
[Introduction to Spring Boot] Submit a form using thymeleaf
How to use CommandLineRunner in Spring Batch of Spring Boot
How to convert A to a and a to A using AND and OR in Java
How to change application.properties settings at boot time in Spring boot
How to develop and register a Sota app in Java
Introducing Spring Boot2, a Java framework for web development (for beginners)
How to create and launch a Dockerfile for Payara Micro
[Rails] How to display error messages for comment function (for beginners)
How to control transactions in Spring Boot without using @Transactional
Introduction to Spring Boot ① ~ DI ~
Introduction to Spring Boot ② ~ AOP ~
Introduction to Spring Boot Part 1
Must-see for beginners! How to manage your Xcode project on Github
[RSpec on Rails] How to write test code for beginners by beginners
How to automatically operate a screen created in Java on Windows
[Tips] How to solve problems with XCode and Swift for beginners
How to set and use profile in annotation-based Configuration in Spring framework
How to load a Spring upload file and view its contents
How to store the information entered in textarea in a variable in the method
Let's write a test code for login function in Spring Boot
How to get JDK 11 on your mac in a comfortable way
How to use Lombok in Spring
How to set Spring Boot + PostgreSQL
How to use ModelMapper (Spring boot)
How to apply thymeleaf changes to the browser immediately with #Spring Boot + maven
Spring.messages.fallback-to-system-locale: false is required to default message.properties for i18n support in Spring boot
How to test a private method in Java and partially mock that method
How to create a query using variables in GraphQL [Using Ruby on Rails]
Workaround for Command Line Runner to work with JUnit in Spring Boot
How to display error messages and success messages when registering as a user
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
(For beginners) [Rails] Time saving tech! How to install and use slim
How to create a server executable JAR and WAR with Spring gradle