** (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
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>
model.addAttribute
to connect and pass to the placeholder of the POST destination as a single character string.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
}
}
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>
URL:http://localhost:8080/hello
Recommended Posts