我的要求是我希望在我的表单字段上使用spring validator类和regex进行验证,所以我在这里做的,我正在验证我的DepartmentName
字段,其中包含字符串,其中没有任何数值。此验证使用正则表达式[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;
}
答案 0 :(得分:0)
您将变量作为类属性找到。验证者是单身人士。移动在validate方法中找到的变量。