【发布时间】:2019-08-30 00:33:07
【问题描述】:
我正在使用 Spring Boot/Thymeleaf 创建一个接受电子邮件地址的表单,重定向到显示已接受电子邮件的结果页面并将其发送到第三方 API(使用 Oauth2 进行身份验证)。我在表单部分遇到问题,我正在尝试使用 Thymeleaf 接受输入以将其显示在 result.html 页面上。尝试在结果页面上显示时收到错误,完整错误是:
[THYMELEAF][http-nio-8080-exec-4] Exception processing template "result.html": Exception evaluating SpringEL expression: "signup.email" (template: "result.html" - line 10, col 4)
我试图按照此处提供的示例进行操作: https://spring.io/guides/gs/handling-form-submission/
我尝试将控制器从 @PostMapping 和 @GetMapping 修改为 @RequestMapping 并添加解决方法中描述的注释,例如:
<!--/*@thymesVar id="signup" type="com.mainconfig.controller1"*/-->
这是包含表单的signup.html 代码:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html>
<head>
<title>My Jmml</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body style="background-color: #2B2B2B">
<br /><br />
<h2 style="text-align:center">Contact Information</h2>
<!-- Input Form -->
<!--/*@thymesVar id="signup" type="com.mainconfig.controller1"*/-->
<form action="#" th:action="@{/signup}" th:object="${signup}" method="post">
<div align="center">
<label>Email Address</label><br /><br />
<!--/*@thymesVar id="email" type="String"*/-->
<input type="text" th:field="*{email}" placeholder="Email" required />
<br />
<br />
<input class="submitbutton" type='submit' value='Submit'/>
<br />
</div>
</form>
</body>
</html>
应该显示电子邮件的结果页面(result.html):
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thank you for your submission!</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Thank you for your submission!</h1>
<p th:text="'Email: ' + ${signup.email}" />
<a href="/index">Submit another message</a>
</body>
</html>
控制器:
@Controller
public class controller1 {
@RequestMapping (value = "/home")
public String home(Model model) {
return "index.html";
}
@RequestMapping(value = "/signup", method= RequestMethod.GET)
public String signupForm(Model model) {
model.addAttribute("signup", new emailInput());
return "signup.html";
}
@RequestMapping(value = "/signup", method= RequestMethod.POST)
public String signupSubmit(@ModelAttribute("email") emailInput email) {
return "result.html";
}
}
预期的输出应该是在注册表单中收集后显示在结果页面上的电子邮件变量。
如果您对如何更好地做我正在尝试的事情有建议,我愿意接受建议!我对 Spring/Thymeleaf 很陌生,但对 Java/Jsp 有经验。感谢您的帮助,如果您需要其他帮助,请告诉我!
【问题讨论】:
-
我非常怀疑您的表单是您的控制器。但是,实际错误在您的
result.html表单中。您有一个名为email的对象,而不是一个名为signup的对象。
标签: java html spring forms thymeleaf