1

我正在尝试验证包含电子邮件的 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 />
4

2 回答 2

5

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

Errorsor参数必须跟在被立即绑定的BindingResult模型对象之后,因为方法签名可能有多个模型对象,并且 Spring 将为BindingResult它们中的每一个创建一个单独的实例,因此以下示例将不起作用:

BindingResult 和@ModelAttribute 的排序无效。

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

请注意,在和之间有一个Model参数。要使其正常工作,您必须重新排序参数,如下所示:PetBindingResult

@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, Model model) { ... }
于 2016-06-08T23:30:18.713 回答
1

您可以使用

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

或这个

<bean id="validator"
  class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
于 2020-05-18T19:58:02.943 回答