Validation Spring表单错误未显示:控制器、命令或模板问题?
我正在尝试创建一个SpringMVCwebapp,我想用Thymeleaf为我的表单添加一些验证。 应用程序正在运行,但验证似乎不起作用,因为没有显示任何内容 这是我的命令类:Validation Spring表单错误未显示:控制器、命令或模板问题?,validation,spring-mvc,thymeleaf,Validation,Spring Mvc,Thymeleaf,我正在尝试创建一个SpringMVCwebapp,我想用Thymeleaf为我的表单添加一些验证。 应用程序正在运行,但验证似乎不起作用,因为没有显示任何内容 这是我的命令类: public class WatchlistItem { private Integer id; @NotBlank(message="Please, enter all the fields") private String title; private String rating; pri
public class WatchlistItem {
private Integer id;
@NotBlank(message="Please, enter all the fields")
private String title;
private String rating;
private String priority;
@Size(max=50, message="Comment should be maximum 50 characters")
private String comment;
我的控制器:
@Controller
public class WatchlistController {
private final List<WatchlistItem> watchlistItems = new ArrayList<>();
private static int index = 1;
@PostMapping("/watchlistItemForm")
public ModelAndView submitWatchlistItemForm(@Valid WatchlistItem watchlistItem, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return new ModelAndView("watchlistItemForm");
}
WatchlistItem existingItem = findWatchlistItemById(watchlistItem.getId());
if (existingItem == null) {
watchlistItem.setId(index++);
watchlistItems.add(watchlistItem);
} else {
existingItem.setComment(watchlistItem.getComment());
existingItem.setPriority(watchlistItem.getPriority());
existingItem.setRating(watchlistItem.getRating());
existingItem.setTitle(watchlistItem.getTitle());
}
RedirectView redirect = new RedirectView();
redirect.setUrl("/watchlist");
return new ModelAndView(redirect);
}
@控制器
公共类监视列表控制器{
私有最终列表watchlistItems=new ArrayList();
私有静态int索引=1;
@后期映射(“/watchlistItemForm”)
公共模型和视图SubmitWatchListForm(@Valid WatchlistItem WatchlistItem,BindingResult BindingResult){
if(bindingResult.hasErrors()){
返回新的ModelAndView(“watchlistItemForm”);
}
WatchlistItem existingItem=findWatchlistItemById(WatchlistItem.getId());
if(existingItem==null){
setId(index++);
添加(watchlistItem);
}否则{
existingItem.setComment(watchlistItem.getComment());
existingItem.setPriority(watchlistItem.getPriority());
existingItem.setRating(watchlistItem.getRating());
existingItem.setTitle(watchlistItem.getTitle());
}
重定向视图重定向=新建重定向视图();
redirect.setUrl(“/watchlist”);
返回新的ModelAndView(重定向);
}
最后是my.html的模板:
<form action="#" method="post" th:action="@{/watchlistItemForm}" th:object="${watchlistItem}">
<h2 class = "mt-4">Submit an item</h2>
<hr/>
<div class = "form-group row ">
<label for = "title" class = "col-sm-2 col-form-label">Title</label>
<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>
</div>
提交项目
标题
我已经看了下面的帖子:,但没有任何效果。
问题是我不知道问题来自何处,控制器、命令或html页面。问题在于控制器代码。您正在破坏模型。使用
返回新的ModelAndVIew(“watchListItemForm”,bindingResult.getModel());"
或者只返回一个字符串
而不是ModelAndView
。我只是尝试了一下,不幸的是它没有改变任何东西。你是否包括了一个验证实现,比如hibernate validator
,或者只是api?不,现在只有api没有实现,验证将无法工作,因为没有任何东西可以进行验证验证。