gpt4 book ai didi

java - SpringMvc 如何根据用户正在执行的功能对对象使用不同的 validator

转载 作者:搜寻专家 更新时间:2023-11-01 01:53:52 26 4
gpt4 key购买 nike

我有一个名为 officers 的对象,我想根据用户想要执行的功能执行不同类型的验证,例如,当一个 officer 记录被注册/保存时,我想执行检查它是否为 NULL 和生成官员编号,当记录正在更新时,我不想不执行此检查并执行更新语句。

但是我在实现这个目标后遇到了问题。我研究了不同的方法,但它不够干净或灵活。我尝试了以下方法,遇到的问题是;

  1. 将已注册的 validator 与 Controller 一起使用,但是每个 Controller 只允许注册一个 validator 。这使得该验证的实现适用于在 Controller 中执行的所有功能。

    1. 使用 Validator Facade 可以允许整个应用程序使用一个验证类,但是它会根据对象的实例类型选择验证,这将每个对象的 validator 数量限制为一个(有待更正)。

如何在不为方法使用单独的 Controller 的情况下对同一对象执行不同的验证。

类主任

   public class Officers implements Serializable{


private String userName;
private String password;
private String password2;
private String fName;
private String lName;
private String oName;
private int divisionNo;
private officerNumber;

OfficerRegistrationValidation 类

    @Component
public class OfficerRegistrationValidation implements Validator {

public boolean supports(Class<?> clazz) {

return Officers.class.equals(clazz);
}

public void validate(Object target, Errors errors) {

Officers officer = (Officers) target;

if (officer.getPassword() == null) {
errors.rejectValue("password", "password.required");
}

if (officer.getPassword2() == null) {
errors.rejectValue("password2", "password2.required");
}
..............
}

Controller

@Controller
public class OfficerController {


@InitBinder("officers")
protected void initBinder(WebDataBinder binder){


//removes white spaces
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));

//formats date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

//By passing true this will convert empty strings to null
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
dateFormat.setLenient(false);


//binder.setValidator(new OfficerRegistrationValidation());
binder.setValidator(officerRegistrationValidation);



}

@RequestMapping(value="officer_registration_save.htm", method = RequestMethod.POST)
public ModelAndView loadPage(HttpServletRequest request,HttpServletResponse response,
@ModelAttribute Officers officer,BindingResult result,ModelMap m, Model model) throws Exception {

if(result.hasErrors()){

return new ModelAndView("officer_registration");

}else

doSave();

}

需要对要更新的记录使用不同类型的验证

 @RequestMapping(value="officer_registration_update.htm", method = RequestMethod.POST)
public ModelAndView loadPage(HttpServletRequest request,HttpServletResponse response,
@ModelAttribute Officers officer,BindingResult result,ModelMap m, Model model) throws Exception {

if(result.hasErrors()){

return new ModelAndView("officer_registration");

}else

doSave();

}

我最终使用的方法是通过 HttpServletRequest 获取按钮值更新或保存,并将其包含在 validator 中以决定是验证更新还是保存。在我寻找最干净和最好的方法之前,有没有人做过类似的事情。到目前为止,我已经决定使用 HttpServletRequest 请求 request.getParameter("action");我发现这种方法有点陈旧而且不干净。

最佳答案

您不需要在 WebDataBinder 中注册您的 validator .相反,您可以创建两个(或任意数量)不同的 Validator满足您的每个要求的类(class)。例如

public class OfficerRegistrationValidation implements Validator {...}

public class OfficerUpdateValidation implements Validator {...}

为每一个创建 bean,或者使用 @Component<bean>声明。将它们注入(inject)你的 @Controller

@Controller
public class OfficerController {
@Inject
private OfficerRegistrationValidation officerRegistrationValidation;

@Inject
private OfficerUpdateValidation officerUpdateValidation;

然后在每个方法中使用您需要的特定方法

@RequestMapping(method = RequestMethod.POST) 
public /* or other return type */ String registerOfficer(@Valid @ModelAttribute Officer officer, BindingResult errors /*, more parameters */) {
officerRegistrationValidation.validate(officer, errors);
if (errors.hasErrors()) {
...// do something
}
...// return something
}

不要在 WebDataBinder 中注册其中任何一个. @Valid将执行默认验证,例如,@NotEmpty@Pattern注释。你的Validator实例将为特定用例执行自定义验证。

关于java - SpringMvc 如何根据用户正在执行的功能对对象使用不同的 validator ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16682992/

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