为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

ITMISS 2022-09-28 14:58:22
我正在使用弹簧启动/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的经验,谢谢你的任何帮助,请让我知道,如果你需要任何其他帮助!
查看完整描述

1 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

希望这将是你的一个起点。确保将 html 文件放在 /资源/模板 下。


我更改了一些您的注册html和结果.html如下,它们仍然不完美(避免使用内联样式并使用外部样式表!


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"

      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">


<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 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" />

        <br />

        <br />

        <input class="submitbutton" type="submit" value="Submit"/>

        <br />

    </div>

</form>

</body>

结果.html看起来像这样


<!DOCTYPE HTML>

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"

      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">

<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: ' + ${email}" />


<a href="/index">Submit another message</a>

</body>

</html>

我还创建了一个表单对象,如果需要,请在此处添加其他字段


public class SignUpForm {

    //you can put some annotations here if you want for validating the email

    //for e.g @NotEmpty or a @Pattern(regexp to validate the email)

    private String email;


    public String getEmail() {

        return email;

    }


    public void setEmail(String email) {

        this.email = email;

    }

}

最后是你的控制器。我将电子邮件从注册帖子请求传递到结果.html通过flash属性:


@Controller

public class Controller1 {


    @RequestMapping(value = "/signup", method= RequestMethod.GET)

    public String signupForm(@ModelAttribute("signup") SignUpForm form) {

        return "/signup";

    }


    @RequestMapping(value = "/signup", method= RequestMethod.POST)

    public String signupSubmit(@ModelAttribute("signup") SignUpForm form, RedirectAttributes redirectAttributes) {

        //validate form first -> check bindingResult documentation


        //do what you need with your form object


        redirectAttributes.addFlashAttribute("email", form.getEmail());

        return "redirect:/result";

    }


    @RequestMapping(value = "/result", method= RequestMethod.GET)

    public String result() {

        return "/result";

    }


}


查看完整回答
反对 回复 2022-09-28
  • 1 回答
  • 0 关注
  • 49 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信