亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

BIG陽 2023-08-09 16:07:04
當我想要轉到 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>
查看完整描述

1 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

您必須將空的 User 對象添加到模型中。


嘗試以下操作:


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

public ModelAndView register(){

    ModelAndView modelAndView = new ModelAndView();


    modelAndView.addObject("User", new User());


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

    return modelAndView;


查看完整回答
反對 回復 2023-08-09
  • 1 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號