如何修复提交变量弹簧/百里叶后“异常评估弹簧表达式”错误

我正在使用弹簧启动/Thymeleaf创建一个接受电子邮件地址的表单,重定向到显示已接受电子邮件的结果页面,并将其发送到第三方API(通过Oauth2进行身份验证)。我在表单部分遇到问题,我正在尝试使用Thymeleaf接受输入以将其显示在结果.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>

应显示电子邮件(结果.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>

预期输出应该是在注册表单中收集后显示在结果页面上的电子邮件变量。


如果您对如何更好地完成我正在尝试的工作有建议,我愿意接受建议!我是春天/百叶的新手,但有Java / Jsp的经验,谢谢你的任何帮助,请让我知道,如果你需要任何其他帮助!


ITMISS
浏览 86回答 1
1回答

手掌心

希望这将是你的一个起点。确保将 html 文件放在 /资源/模板 下。我更改了一些您的注册html和结果.html如下,它们仍然不完美(避免使用内联样式并使用外部样式表!<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"&nbsp; &nbsp; &nbsp; xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"><head>&nbsp; &nbsp; <title>My Jmml</title>&nbsp; &nbsp; <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 th:action="@{/signup}" th:object="${signup}" method="post">&nbsp; &nbsp; <div align="center">&nbsp; &nbsp; &nbsp; &nbsp; <label>Email Address</label><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; <!--/*@thymesVar id="email" type="String"*/-->&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" th:field="*{email}" placeholder="Email" />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; <input class="submitbutton" type="submit" value="Submit"/>&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; </div></form></body>结果.html看起来像这样<!DOCTYPE HTML><html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"&nbsp; &nbsp; &nbsp; xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"><head>&nbsp; &nbsp; <title>Thank you for your submission!</title>&nbsp; &nbsp; <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><h1>Thank you for your submission!</h1><p th:text="'Email: ' + ${email}" /><a href="/index">Submit another message</a></body></html>我还创建了一个表单对象,如果需要,请在此处添加其他字段public class SignUpForm {&nbsp; &nbsp; //you can put some annotations here if you want for validating the email&nbsp; &nbsp; //for e.g @NotEmpty or a @Pattern(regexp to validate the email)&nbsp; &nbsp; private String email;&nbsp; &nbsp; public String getEmail() {&nbsp; &nbsp; &nbsp; &nbsp; return email;&nbsp; &nbsp; }&nbsp; &nbsp; public void setEmail(String email) {&nbsp; &nbsp; &nbsp; &nbsp; this.email = email;&nbsp; &nbsp; }}最后是你的控制器。我将电子邮件从注册帖子请求传递到结果.html通过flash属性:@Controllerpublic class Controller1 {&nbsp; &nbsp; @RequestMapping(value = "/signup", method= RequestMethod.GET)&nbsp; &nbsp; public String signupForm(@ModelAttribute("signup") SignUpForm form) {&nbsp; &nbsp; &nbsp; &nbsp; return "/signup";&nbsp; &nbsp; }&nbsp; &nbsp; @RequestMapping(value = "/signup", method= RequestMethod.POST)&nbsp; &nbsp; public String signupSubmit(@ModelAttribute("signup") SignUpForm form, RedirectAttributes redirectAttributes) {&nbsp; &nbsp; &nbsp; &nbsp; //validate form first -> check bindingResult documentation&nbsp; &nbsp; &nbsp; &nbsp; //do what you need with your form object&nbsp; &nbsp; &nbsp; &nbsp; redirectAttributes.addFlashAttribute("email", form.getEmail());&nbsp; &nbsp; &nbsp; &nbsp; return "redirect:/result";&nbsp; &nbsp; }&nbsp; &nbsp; @RequestMapping(value = "/result", method= RequestMethod.GET)&nbsp; &nbsp; public String result() {&nbsp; &nbsp; &nbsp; &nbsp; return "/result";&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java