我的应用程序无法将表单值从基于Thymeleaf + HTML
的UI绑定到Spring Boot
控制器。
当我在控制器中执行System.out.println时,我将该值变为null
。
的index.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<p>Get your greeting <a href="/greeting">here</a></p>
<form action="/publishMessage" method="post">
<table>
<tr>
<th>CHARGE</th>
</tr>
<tr>
<td>
<textarea th:field="*{messageBody}" name="" cols="90" rows="40">
{
"firstname": "Jose",
"lastname": "Long"
}
</textarea>
</td>
</tr>
<tr>
<td><input type="submit" name="btnSubmit" value="Publish CHARGE message"></td>
</tr>
</table>
</form>
</body>
</html>
PublishMessageController.java
@Controller
public class PublishMessageController {
@PostMapping("/publishMessage")
public String publishMessage(Message message, BindingResult bindingResult, Model model) {
System.out.println("into the publishMessage method..........");
String messageBody = message.getMessageBody();
System.out.println("messageBody: " + messageBody);
return "result";
}
}
Message.java
import lombok.*;
@Data
@Getter
@Setter
@NoArgsConstructor
public class Message {
private String messageBody;
}
输出:
into the publishMessage method..........
messageBody: null
答案 0 :(得分:1)
您的消息永远不会被放入模型中,因此您无法将其用作控制器中的变量。
顺便说一句:该方法不应返回“结果”,而应返回消息正文的字符串。
将您的messageBody放到您展示表单的模型中:
@RequestMapping(value = "/showForm", method=RequestMethod.GET)
public String showForm(Model model) {
...
String messageBody = ...
model.addAttribute("messageBody", messageBody);
...
}
要在您的视图中使用此功能,请在表单中添加th:action
和th:object
:
<form action="#" th:action="@{/publishMessage}" th:object="${messageBody}" method="post">
...
</form>
现在,您可以通过参数中的注释在控制器方法中使用它:
@PostMapping("/publishMessage")
public String publishMessage(@ModelAttribute(value="messageBody") String messageBody, BindingResult bindingResult, Model model) {
...
return messageBody;
}
当然,您可以使用整个消息而不是正文来执行此操作。
答案 1 :(得分:1)
基于SHEIX的上述输入做了一些改动并且有效。
<强> ShowFormController.java 强>
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h3>
<a href="/showForm">Go Back</a>
</h3>
<form action="#" th:action="@{/publishMessage}" th:object="${message}" method="post">
<table>
<tr>
<th>CHARGE</th>
</tr>
<tr>
<td>
<textarea th:field="*{messageBody}" name="" cols="90" rows="40" style="background-color: beige">
</textarea>
</td>
</tr>
<tr>
<td><input type="submit" name="btnSubmit" value="Publish CHARGE message"></td>
</tr>
</table>
</form>
</body>
</html>
<强> show.html 强>
@Data
@Getter
@Setter
@NoArgsConstructor
public class Message {
private String messageBody = "{\n" +
"\t\t\t\t\t\t\"name\": \"John\"\n" +
" }";
}
<强> Message.java 强>
import lombok。*;
@PostMapping("/publishMessage")
public String publishMessage(@ModelAttribute(value = "messageBody") String messageBody, BindingResult bindingResult, Model model) {
System.out.println("into the publishMessage method..........");
System.out.println("messageBody: " + messageBody);
return "result";
<强> PublishMessageController.java 强>
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h3>
Thanks !!!
<a href="/">Go Back</a>
</h3>
</body>
</html>
<强> result.html 强>
databaseMaintenance.child(uid).removeValue();