Thymeleaf + Spring:BindingResult 和 bean 名称“User”

当我想要转到 myweb/register.html 时出现该错误有人可以给我建议我做错了什么吗?在 register.html 中,我突出显示了 ${User}、*{firstName}、*{lastName}、*{email} 和 *{password}


路径 [] 上下文中 servlet [dispatcherServlet] 的 Servlet.service() 抛出异常 [请求处理失败;嵌套异常是org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:“类路径资源[templates/register.html]”)],其根本原因


java.lang.IllegalStateException:BindingResult 和 bean 名称“User”的普通目标对象都不能作为请求属性


控制器:


    @RequestMapping(value = { "/register" }, method = RequestMethod.GET)

public ModelAndView register(){

    ModelAndView modelAndView = new ModelAndView();

    modelAndView.setViewName("register"); // resources/templates/register.html

    return modelAndView;

注册.html:

<!DOCTYPE html>

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

<head>

    <title>Registration Form</title>

    <!-- link rel="stylesheet" type="text/css" th:href="@{/css/registration.css}" /-->

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>

<body style="background-color: #ededed;">

<div style="background-color: #337ab7; height: 50px;"></div>

<div class="container-fluid" style="margin-top: 30px;">


    <div class="row col-lg-4 col-lg-offset-4" style="margin-top: 40px; background-color: #fff; padding: 20px; border: solid 1px #ddd;">

        <form autocomplete="off" action="#" th:action="@{/register}" th:object="${User}" method="POST" class="form-signin" role="form">

            <h3 class="form-signin-heading">Registration Form</h3>

            <div class="form-group">

                <div class="">

                    <input type="text" th:field="*{firstName}" placeholder="Name" class="form-control" />

                </div>

BIG阳
浏览 132回答 1
1回答

慕标琳琳

您必须将空的 User 对象添加到模型中。尝试以下操作:&nbsp; &nbsp; @RequestMapping(value = { "/register" }, method = RequestMethod.GET)public ModelAndView register(){&nbsp; &nbsp; ModelAndView modelAndView = new ModelAndView();&nbsp; &nbsp; modelAndView.addObject("User", new User());&nbsp; &nbsp; modelAndView.setViewName("register"); // resources/templates/register.html&nbsp; &nbsp; return modelAndView;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java