我的要求是我希望使用 spring 验证器类和正则表达式对我的表单字段进行验证,所以我在这里做什么,我正在验证我DepartmentName
在 String 中的字段在任何地方都不包含数值。我使用正则表达式执行此验证,[0-9]
因为如果它包含任何数值,则 matcher.find()
如果它返回true,则返回true,我正在抛出错误消息。所以我面临的问题是,当我为字符串提供非数值验证时,如果我提供纯字符串,那么如果我运行应用程序,它仍然会抛出相同的消息再次提供纯字符串值然后它的工作,但如果我再次提供错误的条目,验证正在发生,但之后如果我提供正确的条目相同的消息抛出所以每次我需要运行我的应用程序时请解决这个问题
这是我的验证器类
package com.ankur.tutorial.validator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import com.nousinfo.tutorial.service.model.DepartmentBO;
public class DepartmentValidator implements Validator {
boolean found = false;
public boolean supports(Class<?> arg0) {
return DepartmentBO.class.isAssignableFrom(arg0);
}
public void validate(Object object, Errors errors) {
DepartmentBO departmentBO = (DepartmentBO) (object);
System.out.println(departmentBO.getDepartmentName());
if (departmentBO.getDepartmentName().equals("")) {
errors.rejectValue("departmentName", "department.Name");
} else {
Pattern pattern = Pattern.compile("[0-9]");
Matcher matcher = pattern.matcher(departmentBO.getDepartmentName());
while (matcher.find()) {
found = true;
}
System.out.println(found);
if (found) {
errors.rejectValue("departmentName", "department.string");
}
}
}
}
这是我的控制器
package com.nousinfo.tutorial.controllers;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
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.servlet.ModelAndView;
import com.nousinfo.tutorial.model.DepartmentForm;
import com.nousinfo.tutorial.service.impl.DepartmentServiceImpl;
import com.nousinfo.tutorial.service.model.DepartmentBO;
import com.nousinfo.tutorial.validator.DepartmentValidator;
@Controller
@RequestMapping("departmentController")
public class DepartmentController {
private DepartmentServiceImpl departmentServiceImpl;
private DepartmentValidator departmentValidator;
public DepartmentServiceImpl getDepartmentServiceImpl() {
return departmentServiceImpl;
}
public void setDepartmentServiceImpl(
DepartmentServiceImpl departmentServiceImpl) {
this.departmentServiceImpl = departmentServiceImpl;
}
public DepartmentValidator getDepartmentValidator() {
return departmentValidator;
}
public void setDepartmentValidator(DepartmentValidator departmentValidator) {
this.departmentValidator = departmentValidator;
}
/**
* Set to set the view
*
* @param model
* @return
* @throws Exception
*/
@RequestMapping(value = "/departmentForm", method = RequestMethod.GET)
public String view(Model model) throws Exception {
DepartmentBO departmentBO = new DepartmentBO();
model.addAttribute("departmentBO", departmentBO);
return "departmentForm";
}
/**
* Create the department
*
* @param departmentForm
* @param bindingResult
* @param model
* @return
* @throws Exception
*/
@RequestMapping(value = "/createDepartment", method = RequestMethod.POST)
public ModelAndView createEmployee(
@ModelAttribute("departmentBO") DepartmentBO departmentBO,
BindingResult bindingResult) throws Exception {
ModelAndView modelAndView = new ModelAndView();
departmentValidator.validate(departmentBO, bindingResult);
if (bindingResult.hasErrors()) {
modelAndView.setViewName("departmentForm");
return modelAndView;
}
modelAndView.addObject("departmentBO", departmentBO);
if (departmentBO.getUpdateStatus() == 'A') {
boolean flag = departmentServiceImpl.actionDecider(departmentBO);
if (flag == false)
modelAndView.setViewName("DBError");
else
modelAndView.setViewName("Success");
}
return modelAndView;
}