为什么Spring BindingResult或验证器不显示任何错误?

时间:2016-06-08 16:55:08

标签: java spring spring-mvc spring-validator

我正在尝试验证包含电子邮件的Spring Bean,但是当请求Bean中的电子邮件为空字符串时,验证器和BindingResult都不会显示任何错误。 请参阅以下代码:

豆:

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springmodules.validation.bean.conf.loader.annotation.handler.Email;
import org.springmodules.validation.bean.conf.loader.annotation.handler.NotEmpty;

@Component("grouponRedemptionFormBean")
@Scope("prototype")
public class GrouponRedemptionBean {

@NotEmpty(message = "Please enter your email addresss.")
@Email(message = "Please correct your email.")
private String  email;
   …
}

控制器:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
public class GrouponVoucherRedemptionController {

@Autowired
@Qualifier("defaultBeanValidator")
private Validator validator;

@RequestMapping(value="/groupon-redemption.ep", method=RequestMethod.POST)
public String PostGrouponRedemption(@Valid @ModelAttribute GrouponRedemptionBean grouponRedemptionBean, BindingResult bindingResult,
HttpServletRequest request, HttpServletResponse response, Model model){

    Errors errors = new BeanPropertyBindingResult(grouponRedemptionBean, "grouponRedemptionFormBean");
    validator.validate(grouponRedemptionBean, errors);
    if(errors.hasErrors()) {
        bindingResult.addAllErrors(errors);
    }
    if (bindingResult.hasErrors()) {
        return GROUPON_REDEMPTION_VIEW;
    }    
...

XML配置:

<mvc:annotation-driven />

2 个答案:

答案 0 :(得分:2)

您的参数顺序错误 - BindingResult必须紧跟在模型对象之后(在您的情况下为GrouponRedemptionBean)。请参阅documentation

  

ErrorsBindingResult参数必须遵循立即绑定的模型对象,因为方法签名可能具有多个模型对象,而Spring将创建单独的BindingResult实例对于他们每个人,所以以下样本将无法工作:

     

BindingResult和@ModelAttribute的排序无效。

@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet, Model model, BindingResult result) { ... }
     

请注意,ModelPet之间存在BindingResult参数。要使其正常工作,您必须按如下方式重新排序参数:

@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, Model model) { ... }

答案 1 :(得分:1)

您可以使用

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

或者这个

<bean id="validator"
  class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>