gpt4 book ai didi

java - 为什么 Spring BindingResult 或 validator 不显示任何错误?

转载 作者:行者123 更新时间:2023-11-30 02:54:18 24 4
gpt4 key购买 nike

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

bean :

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;

}

Controller :

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 />

最佳答案

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

The Errors or BindingResult parameters have to follow the model object that is being bound immediately as the method signature might have more than one model object and Spring will create a separate BindingResult instance for each of them so the following sample won't work:

Invalid ordering of BindingResult and @ModelAttribute.

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

Note, that there is a Model parameter in between Pet and BindingResult. To get this working you have to reorder the parameters as follows:

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

关于java - 为什么 Spring BindingResult 或 validator 不显示任何错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37708548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com