【问题标题】:How to fix "Exception evaluating SpringEL expression" error after submitting a variable Spring/Thymeleaf提交变量 Spring/Thymeleaf 后如何修复“异常评估 SpringEL 表达式”错误
【发布时间】: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


【解决方案1】:

希望这将是您的起点。 确保将 html 文件放在 /resources/templates 下。

我对您的注册 html 和 result.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>

result.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;
    }
}

最后是你的控制器。我通过 flash 属性将注册发布请求中的电子邮件传递给 result.html:

@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";
    }

}

【讨论】:

  • 谢谢!这解决了我的问题,看看你是如何设置的,看起来我最大的问题是结果页面上的 signup.email 以及你对控制器做了什么!我确实有一个表单对象,我不确定我需要哪些文件来获得有关问题的帮助。再次感谢你!如果您确实阅读了这篇文章,是在注册的 .post 控制器内,我想将 http 调用放在我正在使用的第三方 api 中吗?
猜你喜欢
  • 2017-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多