我无法使用Thymeleaf和Spring Boot呈现HTML页面。尝试将html文件中的字段标记为类中的字段时出现错误。
错误是:org.thymeleaf.exceptions.TemplateProcessingException:执行处理器org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagTag时出错(模板:“ userPreview”-第10行,第32行)
HTML模板:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Email User Preview</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Send E-mail:</h1>
<form action="#" th:action="@{/sendmail}" th:object="${message}" method="post">
<p>To:: <input type="text" th:field="*{receiverEmail}" /></p>
<p>Subject: <input type="text" th:field="*{subject}" /></p>
<p>Message: <input type="text" th:field="*{message}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
控制器:
@Controller
public class TestController {
@GetMapping("/test")
public String send() {
user.setEmailAddress("yasseen.salama@gmail.com");
try {
emailService.sendMail(user, "Hello", "Test");
} catch (MailException mailException) {
System.out.println(mailException);
}
return "Email sent.";
}
@GetMapping("/sendmail")
public String sendingMail(Model model) {
Message message = new Message();
model.addAttribute("userPreview", message);
return "userPreview";
}
@PostMapping("/sendmail")
public String mailSubmit(@ModelAttribute Message message) {
return "Result";
}
}
班级留言:
public class Message {
String receiverEmail;
String subject;
String message;
public String getReceiverEmail() {
return receiverEmail;
}
public void setReceiverEmail(String receiverEmail) {
this.receiverEmail = receiverEmail;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
答案 0 :(得分:1)
模板中要使用的对象或变量名称为userPreview
,而不是message
,因为这是Model对象中的键