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

Screen transition to hello.html with GET method

@Controller

@GetMapping

HelloController.java


package com.example.demo.trySpring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController{
    @GetMapping("/hello")
    public String getHello(){
        return"hello";  //hello.Screen transition to html
    }
}

hello.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"> <!--Described to use Thymeleaf-->
<head>
    <meta charset="UTF8"></meta>
    <title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
<body>
</html>

Access the URL and check!

HW.png

Pass the value from the screen with the POST method

Namespace

//Xmlns:th = “URL” //Definition destination
<input type="text" name="text1" th:value="${text1_value}"/>

hello.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF8"></meta>
    <title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
<form method="post" action="/hello">
Enter your favorite characters:
    <input type="text" name="text1" th:value="${text1_value}"/>
    <input type="submit" value="click"/>
</form>
<body>
</html>

Specify the method to be used in the method attribute of the form tag

@PostMapping

@RequestParam

model.addAttribute

In the example below

HelloController.java


package com.example.demo.trySpring;
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")
    public String getHello(){
        return"hello";
    }
    @PostMapping("/hello")
    public String postRequest(@RequestParam("text1")String str, Model model){
        model.addAttribute("sample",str); //Register the character string received from the screen in Model
        return "helloResponse"; //helloResponse.Screen transition to html
    }
}

Receives a value from the controller class

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-->
<p th:text="${sample}"></p><body>
</html>

Access the URL and check!

post1.png

post2.png

(Note) What is Schema?

(Note) What is the Thymeleaf model?

Recommended Posts

[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
Screen transition by Post method [Java]
Get stuck in a Java primer
How to request a CSV file as JSON with jMeter
[Java] Throw a request and display the screen (GET / POST)
Create a playground with Xcode 12
Draw a gradient with CAGradientLayer
A story stuck with NotSerializableException
Things to forget when intercepting a request with Android's WebView # shouldInterceptRequest
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
[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.