** (Objectif) Incorporez la valeur POSTÉE (prénom et nom) dans le formulaire html et affichez-la à l'écran **
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">
Nom de famille:
<input type="text" name="text1" th:value="${text1_value}"/>
<br>
Nom:
<input type="text" name="text2" th:value="${text2_value}"/>
<br><br>
<input type="submit" value="Cliquez sur"/>
<br>
</form>
</body>
</html>
model.addAttribute
pour vous connecter et passer à l'espace réservé de la destination POST sous la forme d'une chaîne de caractères unique.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){
//Enregistrez la chaîne de caractères reçue de l'écran HTML dans Modèle
model.addAttribute("userName","je"+ text1 +" "+ text2+"est.");
return "helloResponse"; //helloResponse.Transition d'écran vers 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>
<!--
Reçoit la valeur du modèle et affiche les caractères reçus
La valeur spécifiée dans l'espace réservé(userName)Mettre en
-->
<p th:text="${userName}"></p><body>
</html>
URL:http://localhost:8080/hello
Recommended Posts