You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Spring Boot Validation 校验错误消息无法正常显示问题求助

问题原因

  • 实体类属性命名不符合JavaBean小驼峰规范:你定义的Title字段首字母大写,校验注解产生的错误信息关联的字段名为Title,但前端Thymeleaf绑定的是小写开头的title,两者不匹配导致错误信息找不到对应展示位置。
  • 校验失败返回视图时未携带表单对象:你当前校验失败时直接返回new ModelAndView("watchlistItemForm"),模型中没有存入watchlistItem对象和绑定的校验错误信息,Thymeleaf无法读取错误内容。
  • (可选遗漏问题)表单标签未正确绑定表单对象:如果你的<form>标签没有配置th:object="${watchlistItem}"属性,*{title}这类字段绑定语法也无法正常生效。
  • 实体类未补充Getter/Setter方法:你贴出的代码中没有展示WatchlistItem类的Getter、Setter方法,Spring MVC参数绑定和Thymeleaf字段读取都依赖这两个方法,缺失会导致绑定失败。

解决方案

  1. 修正实体类属性命名,补充Getter/Setter方法
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

public class WatchlistItem {
    @NotBlank(message="Please enter the title")
    private String title;
    private String rating;
    private String priority;

    @Size(max=50, message="Comment should be maximum of 50 characters")
    private String comment;
    private Integer id;

    // 补充所有字段的Getter和Setter方法
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getRating() {
        return rating;
    }
    public void setRating(String rating) {
        this.rating = rating;
    }
    public String getPriority() {
        return priority;
    }
    public void setPriority(String priority) {
        this.priority = priority;
    }
    public String getComment() {
        return comment;
    }
    public void setComment(String comment) {
        this.comment = comment;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
}
  1. 修正Controller校验失败的返回逻辑,携带表单对象到视图
@PostMapping("/watchlistItemForm")
public ModelAndView getWatchlistItem(@Valid WatchlistItem watchlistItem, BindingResult bindingResult) {
    if(bindingResult.hasErrors()) {
        ModelAndView mav = new ModelAndView("watchlistItemForm");
        mav.addObject("watchlistItem", watchlistItem);
        return mav;
    }
    
    WatchlistItem exisiting = findWatchlistItem(watchlistItem.getId());
    if (exisiting == null) {
        watchlistItem.setId(index++);
        watchlistItems.add(watchlistItem);
    }else {
        // 现有对象更新逻辑
    }
                
    RedirectView view = new RedirectView();
    view.setUrl("/watchlist");
    return new ModelAndView(view);
}
  1. 确认表单标签正确绑定对象
    在你的HTML表单标签中加上th:object属性,示例如下:
<form th:action="@{/watchlistItemForm}" th:object="${watchlistItem}" method="post">
    <!-- 原有表单内容保持不变 -->
    <div class = "col-sm-4">
        <input th:field="*{title}" type = "text" class = "form-control" placeholder = "Mandatory">
    </div>
    <div class="col-sm-4">
        <span class="text-danger" th:errors="*{title}"> </span>      
    </div>
</form>

内容的提问来源于stack exchange,提问作者Nick Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 11:06:03