【问题标题】:Getting Error when trying to perform form validation with spring and thymeleaf尝试使用 spring 和 thymeleaf 执行表单验证时出错
【发布时间】:2018-11-23 11:59:01
【问题描述】:

我正在尝试使用 spring 和 Thymeleaf 执行表单验证,但是在导航到表单 URL 时出现以下错误:

出现意外错误(类型=内部服务器错误, 状态=500)。处理器执行期间出错 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (模板:“create-store-form” - 第 18 行,第 28 列)

控制器

@RequestMapping(value="/createStoreForm.html")
public ModelAndView getCreateStoreForm() {
    ModelAndView modelAndView = new ModelAndView("create-store-form");
    return modelAndView;
}

@RequestMapping(value="/submitNewStoreForm" , method = RequestMethod.POST)
public ModelAndView submitNewStoreForm(@Valid @ModelAttribute("newStore") Store store, BindingResult bindingResult, HttpSession httpSession, Principal principal) {
    String loggedInUsername = principal.getName();
    User loggedInUser = userService.findUser(loggedInUsername);
    ModelAndView modelAndView;
    if(bindingResult.hasErrors()) {
        modelAndView = new ModelAndView("create-store-form");
        return modelAndView;
    }       
    try {
        store.setSellerName(loggedInUser.getUsername());
        storeService.saveStore(store);
    }catch (Exception e) {
        modelAndView = new ModelAndView("create-store-form");
        System.out.println("User '"+store.getName()+"' already exist on db.");
        bindingResult.addError(new ObjectError("dup", "* Store '"+store.getName()+"' already exist!"));
        return modelAndView;
    }
    modelAndView = new ModelAndView("store-page");
    modelAndView.addObject("storeName", store.getName());

    return modelAndView;

}

HTML

<div class="row">

    <form action="#" th:action="@{/submitNewStoreForm}" th:object="${newStore}" method="post">
    <table>
        <tr>
            <td>Name:</td>
            <td><input type="text" th:field="*{name}" /></td>
            <td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Title error message</td>
        </tr>
        <tr>
            <td>Image Link:</td>
            <td><input type="text" th:field="*{imageLink}" /></td>
            <td th:if="${#fields.hasErrors('imageLink')}" th:errors="*{imageLink}">Content error message</td>
        </tr>
        <tr>
            <td><button type="submit">Submit post</button></td>
        </tr>
    </table>
</form>


</div>

我做错了什么?我关注了this guide

【问题讨论】:

    标签: java html spring-mvc thymeleaf


    【解决方案1】:

    可能发生此错误的原因有多种。

    首先尝试以下操作:在getCreateStoreForm() 中,替换

    ... = new ModelAndView("create-store-form”);
    

    ... 与:

    ... = new ModelAndView("create-store-form”, “newStore”, new Store());
    
    1. "create-store-form” = html 表单视图名称
    2. “newStore” = 对象名称(th: thymeleaf 中的对象)
    3. new Store() = 对模型对象的引用

    请让我知道这是否有效。如果没有,请发布完整的堆栈跟踪和Store 的类。

    【讨论】:

      猜你喜欢
      • 2020-01-13
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 2017-07-14
      • 2020-09-18
      • 1970-01-01
      相关资源
      最近更新 更多