【问题标题】:Invalid target for Validator spring error?Validator spring 错误的目标无效?
【发布时间】: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>

第一次在这里发帖,我不是特别常去这个网站,如果我的格式被关闭了,很抱歉。

【问题讨论】:

    标签: java spring jsp


    【解决方案1】:

    为@InitBinder注解指定值参数:

    @InitBinder("show")
    

    有关详细信息,请参阅Annotation Type InitBinder 文档:

    默认应用于所有命令/表单属性和所有由带注释的处理程序类处理的请求参数。在此处指定模型属性名称或请求参数名称会将 init-binder 方法限制为那些特定的属性/参数,不同的 init-binder 方法通常适用于不同的属性或参数组。

    【讨论】:

      猜你喜欢
      • 2017-10-27
      • 1970-01-01
      • 2014-03-07
      • 2022-09-26
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      相关资源
      最近更新 更多