【发布时间】:2021-02-22 15:23:01
【问题描述】:
每当我尝试在线访问“/shows”网址时都会收到以下错误:
java.lang.IllegalStateException: Invalid target for Validator [co2103.hw1.controller.ShowValidator@7f9fc01d]: co2103.hw1.domain.Channel@72edafb5
代码并不复杂,只是不知道哪里出了问题,希望大家帮忙。在我为 Show 类实现验证器之前,代码一切正常
控制器
@Controller
public class ShowController {
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.addValidators(new ShowValidator());
}
@GetMapping("/shows")
public String listShows(@RequestParam(name = "channel") int passedID, Model model) {
Channel channel = new Channel();
List<Channel> channels = Hw1Application.channels;
for (Channel ch : channels) {
if (ch.getId() == passedID) {
channel = ch;
}
}
List<Show> shows = channel.getShows();
model.addAttribute("channel", channel);
model.addAttribute("shows", shows);
return "/shows/list";
}
@RequestMapping("/newShow")
public String goToAddShowForm(@RequestParam(name = "channel") int passedID, Model model) {
Channel channel = new Channel();
List<Channel> channels = Hw1Application.channels;
for (Channel ch : channels) {
if (ch.getId() == passedID) {
channel = ch;
}
}
Show show = new Show();
model.addAttribute("show", show);
model.addAttribute("channel", channel);
return "/shows/form";
}
@PostMapping("addShow")
public String addShow(@Valid @ModelAttribute Show show, BindingResult result, @RequestParam(name = "channel") int passedID) {
if (result.hasErrors()) {
return "/show/form";
}
Channel channel = null;
for (Channel ch : Hw1Application.channels) {
if (ch.getId() == passedID) {
channel = ch;
break;
}
}
List<Show> shows = channel.getShows();
shows.add(show);
channel.setShows(shows);
return "redirect:/channels";
}
}
验证器
public class ShowValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return Show.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
Show show = (Show) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "", "Title cannot be empty!");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "actors", "", "Actors cannot be empty!");
List<String> validCategories = new ArrayList<>();
validCategories.addAll(Arrays.asList("Comedy", "Documentary", "Drama"));
if (!validCategories.contains(show.getCategory())) {
errors.rejectValue("category", "", "Category must be either 'Comedy', 'Drama' or 'Documentary'");
}
if (show.getEpisodes() <= 4 || show.getEpisodes() > 8) {
errors.rejectValue("episodes", "", "Episodes must be between (including) 5 and 8");
}
}
}
jsp
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h2>Add new show for ${channel.name}</h2>
<form:form action = "addShow?channel=${channel.id}" modelAttribute = "show">
<form:label path = "title">Title of show </form:label><form:input path = "title"/>
<form:errors path = "title"/><br/>
<form:label path = "actors">Actors in show </form:label><form:input path = "actors"/>
<form:errors path = "actors"/><br/>
<form:label path = "category">Show category </form:label><form:input path = "category"/>
<form:errors path = "category"/><br/>
<form:label path = "episodes">How many episodes? ></form:label><form:input path = "episodes"/>
<form:errors path = "episodes"/><br/>
<input type = "submit">
</form:form>
</body>
</html>
第一次在这里发帖,我不是特别常去这个网站,如果我的格式被关闭了,很抱歉。
【问题讨论】: