[Java] Throw a request and display the screen ② (GET / POST)

Make a request and display the result on the screen (thymeleaf)

** (Goal) Embed the POST value (first and last name) in the html form and display it on the screen **

Project Root
└─src
    └─ main
        └─ java  
            └─ com.example
                └─ demo
                    └─trySpring
                       └─HelloController.java
Project Root
└─src
    └─ main
        └─ resources
            └─templates
                 └─hello.html
                
                 └─helloResponse.html

Screen transition to hello.html with GET

hello.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello World</title>
</head>
<body>
    <h1>Hello World</h1>
    <form method="post" action="/hello">
Last name:
        <input type="text" name="text1" th:value="${text1_value}"/>
        <br>
name:
        <input type="text" name="text2" th:value="${text2_value}"/>
        <br><br>
        <input type="submit" value="click"/>
        <br>
    </form>
</body>
</html>

1.png

Create controller class (HelloController) to POST

HelloController.java


import lombok.Getter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {
    @GetMapping("/hello")
    private String getHello() {
        return "hello";
    }
    @PostMapping("/hello")
    public String postRequest(@RequestParam("text1")String text1,@RequestParam("text2")String text2, Model model){
        //Register the character string received from the HTML screen in Model
        model.addAttribute("userName","I"+ text1 +" "+ text2+"is."); 
        return "helloResponse"; //helloResponse.Screen transition to html
    }
}

Create helloResponse.html of POST destination

helloResponse.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF8"></meta>
    <title>Response Sample</title>
</head>
<body>
<h1>Hello Response</h1>
<!--
Receives the value from Model and displays the received characters
The value specified in the placeholder(userName)Put in
-->
<p th:text="${userName}"></p><body>
</html>

Access the URL and check!

URL:http://localhost:8080/hello 0.png

2.png

Recommended Posts

[Java] Throw a request and display the screen ② (GET / POST)
[Java] Throw a request and display the screen (GET / POST)
Get the result of POST in Java
[Java8] Search the directory and get the file
[Java] How to get the current date and time and specify the display format
[Java] Get and display the date 10 days later using the Time API added from Java 8.
[Java] How to get a request by HTTP communication
Memorandum No.4 "Get a character string and decorate it" [Java]
Let's make a calculator application in Java ~ Display the application window
[Java] Get the dates of the past Monday and Sunday in order
I want to return to the previous screen with kotlin and java!
Java, JS (jQuery) and Ajax. Get a specific value of JSON.
Get the public URL of a private Flickr file in Java
Let's create a TODO application in Java 5 Switch the display of TODO
[Java] How to convert from String to Path type and get the path
Throw a PATCH request with HttpURLConnection
Screen transition by Post method [Java]
Get stuck in a Java primer
A collection of phrases that impresses the "different feeling" of Java and JavaScript
The story of forgetting to close a file in Java and failing
A note on the differences between interfaces and abstract classes in Java
[Java] Create a jar file with both compressed and uncompressed with the jar command
Interpret the relationship between Java methods and arguments into a biochemical formula
Get a rough idea of the differences between protocols, classes and structs!
[Java] How to get to the front of a specific string using the String class
How to get the absolute path of a directory running in Java
<java> Split the address before and after the street address with a regular expression
How to create your own annotation in Java and get the value
Display Japanese calendar and days of the week using java8 standard class
[Java] Create and apply a slide master
A look at Jenkins, OpenJDK 8 and Java 11
Display an image on the base64 screen
[Java] Get the day of the specific day of the week
[Java] How to get the current directory
A Java engineer compared Swift, Kotlin, and Java.
How to get the date in java
[Java 7] Divide the Java list and execute the process
Fix the view screen of the post page
[Java] Get the date with the LocalDateTime class
Let's make a calculator application with Java ~ Create a display area in the window
[Java] Program example to get the maximum and minimum values from an array
[Ruby] Sending a POST HTTP request to the Web API using OAuth authentication
Create a Yellowfin custom formatter and display the minus of the number as △ (triangle)
Find out about Rails hidden_field (create a confirmation screen and check the behavior)
I want to recursively get the superclass and interface of a certain class
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.