java – SpringMvc如何根据用户正在执行的函数为Object使用不同的验证器
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – SpringMvc如何根据用户正在执行的函数为Object使用不同的验证器,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3877字,纯文字阅读大概需要6分钟。
内容图文
![java – SpringMvc如何根据用户正在执行的函数为Object使用不同的验证器](/upload/InfoBanner/zyjiaocheng/803/724d30a2502e4f10a2a69517c3809833.jpg)
我有一个名为“人员”的对象我希望根据用户想要执行的功能执行不同类型的验证,例如,当正在注册/保存人员记录时,我想要检查是否为NULL并生成一名官员数字以及更新记录时我不想执行此检查并执行更新语句.
但是,从那以后我遇到了问题.我看过不同的方法,它不够干净或灵活.我尝试了以下方法,面临的问题是;
>在Controller上使用已注册的验证器,但每个Controller只允许注册一个验证器.这使得该验证的实现适用于在控制器中执行的所有功能.
>使用Validator Facade可以为整个应用程序提供一个验证类,但是它会根据对象的实例类型选择验证,这会将每个对象的验证器数量限制为一个(需要更正).
如何在不为该方法使用单独的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
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更新或保存按钮值,并在验证器中包含该值以决定是否验证更新或保存.在我寻找最干净,最好的方法之前,有没有人做过类似的事情.到目前为止我已经决定使用HttpServletRequest请求request.getParameter(“action”);我发现这种方法有点陈旧而且不干净.
解决方法:
您无需在WebDataBinder中注册验证器.相反,您可以为每个需求创建两个(或任意数量)不同的Validator类.例如
public class OfficerRegistrationValidation implements Validator {...}
public class OfficerUpdateValidation implements Validator {...}
使用@Component或< bean>创建每个bean的bean.宣言.在@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如何根据用户正在执行的函数为Object使用不同的验证器全部内容,希望文章能够帮你解决java – SpringMvc如何根据用户正在执行的函数为Object使用不同的验证器所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。