Skip to main content
 首页 » 编程设计

spring-mvc之使用@InitBinder 设置时似乎没有使用验证器

2025年05月04日18qq78292959

我正在设置@InitBinder 来为Spring MVC Controller 设置验证器。但是,看起来验证器实际上并没有在运行时触发。

Controller 看起来像这样:

@Controller 
@RequestMapping("/login") 
public class LoginController { 
 
final private static String USER_COOKIE_NAME = "ADVPROT_CHAT_USER";  
final private static String CURRENT_VIEW     = "login"; 
final private static String SUCCESS_VIEW     = "redirect:welcome.htm"; 
 
@Autowired 
private UserManagerService userManagerService; 
 
@Autowired 
private LoginValidator loginValidator; 
 
@InitBinder("login") 
protected void initBinder(WebDataBinder binder) { 
    binder.setValidator(new LoginValidator()); 
} 
 
@RequestMapping(method = RequestMethod.POST) 
protected String processSubmit(@Valid @ModelAttribute("login") Login login, BindingResult result, ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception { 
 
    if(result.hasErrors()) { 
        return CURRENT_VIEW; 
    } else { 
        model.addAttribute("login", login); 
 
        String loginResultMessage = "Login successful via LDAP";   
        User user = getUser(login.getUsername()); 
        model.addAttribute("userLoggedIn", user); 
        model.addAttribute("loginResultMessage", loginResultMessage); 
 
        request.getSession().setAttribute("userLoggedIn", login.getUserLoggingIn()); 
        if (login.getUserLoggingIn() != null) { 
            response.addCookie(new Cookie(USER_COOKIE_NAME, login.getUserLoggingIn().getId())); 
        } 
 
        return SUCCESS_VIEW; 
    } 
} 
 
private User getUser(String username) throws Exception { 
 
    return userManagerService.getUserById(username); 
} 
 
@RequestMapping(method = RequestMethod.GET) 
protected String initForm(ModelMap model, HttpServletRequest request) { 
    Login login = new Login(); 
 
    Cookie[] cookies = request.getCookies(); 
    if (cookies != null && cookies.length > 0) { 
        for (Cookie nextCookie: cookies ) { 
            if (nextCookie.getName().equals(USER_COOKIE_NAME)) { 
                login.setUsername(nextCookie.getValue()); 
                break; 
            } 
        } 
    } 
    model.addAttribute("login", login); 
    return CURRENT_VIEW; 
} 
}  

在运行时,验证器似乎没有进行任何检查。

如果我在不指定模型属性的情况下使用@InitBinder

@InitBinder 
protected void initBinder(WebDataBinder binder) { 
    binder.setValidator(new LoginValidator()); 
} 

看起来验证器在其他对象上被触发了,我得到了异常。所以,我的猜测是我为 @InitBinder 指定模型的方式在某种程度上是不正确的,但我不确定。

请您参考如下方法:

您不应该在 InitBinder 中创建验证器的新实例。它必须看起来像:

@Autowired 
private LoginValidator loginValidator; 
 
@InitBinder 
public void InitBinder(WebDataBinder binder) { 
    binder.setValidator(loginValidator); 
} 

由于您正在使用验证器,因此您必须使用 @Validated 而不是 @Valid。我的意思是:

@RequestMapping(method = RequestMethod.POST) 
protected String processSubmit(@Validated @ModelAttribute("login") Login login, BindingResult result, ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception { 

如果您需要一个工作示例,请查看 here