我有两个模型:User,Project
public class Project{
private int id;
@NotEmpty(message="Project Name can not be empty")
private String name;
private User manager;
private User operator;
//getter/setter omitted
}
public class User{
private int id;
private String name;
//omit other properties and getter/setter
}
现在,当我创建一个新项目时,我将向ProjectController提交以下参数:
projects?name=jhon&manager.id=1&operator.id=2...
然后我将创建一个新的Project对象并将其插入db。
但是我必须验证管理器的id和运算符是否有效,也就是说如果用户表中存在匹配的id,我将验证它。
所以我想知道如何实现这种验证?
update1:使用验证器
这是创建新项目的表格:
<sf:form method="${project.id==0?'post':'put'}" commandName="project" action="${context}${action}">
Manager:<sf:input path="manager.id" /> <sf:errors path="manager.id" /> <br />
Operator:<sf:input path="operator.id" /> <sf:errors path="operator.id" /> <br />
Name:<sf:input path="name" /> <sf:errors path="name" /> <br />
<input type="submit" value="Submit" />
</sf:form>
@Override
public void validate(Object obj, Errors errors) {
User user = (User) obj;
int id=user.getId();
User u=userDao.query(id);
if(u==null){
errors.rejectValue("id", "user does not exist!");
}
}
似乎这个验证器有效。
但是,错误消息无法在表单中显示。
然后通过调试我检查结果对象,我发现了这个:
org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'project' on field 'id': rejected value [0]; codes [user does not exist!.project.id,user does not exist!.id,user does not exist!.int,user does not exist!]; arguments []; default message [null]
Field error in object 'project' on field 'id': rejected value [0]; codes [user does not exist!.project.id,user does not exist!.id,user does not exist!.int,user does not exist!]; arguments []; default message [null]
似乎结果确实有错误,但它的路径是project.id
,而在我的表单中它是project.manager.id
如何解决?
答案 0 :(得分:3)
这是一种可能的解决方案。
创建以下课程:
...
import org.springframework.validation.Validator;
...
@Component
public class ProjectValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return Project.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
Project project = (Project) target;
/* Do your checks here */
...
if (managerIdDoesNotMatch) {
errors.rejectValue("manager.id", "your_error_code");
}
...
if (operatorIdDoesNotMatch) {
errors.rejectValue("operator.id", "your_error_code");
}
...
}
}
在您的控制器中执行以下操作:
...
public class ProjectController {
@Autowired
ProjectValidator projectValidator;
...
@RequestMapping(...)
public String yourCreateMethod(..., @ModelAttribute @Valid Project project, BindingResult result) {
projectValidator.validate(project, result);
if (result.hasErrors()){
// do something
}
else {
// do something else
}
}
}
这应该让你开始。你可以用不同的方式实例化/设置验证器,拥有一个用户子验证器,但你可以得到一般的想法。
参考文献:
答案 1 :(得分:2)
实际上您需要做的是在
上添加@Valid
private User manager;
private User operator;
像这样
@Valid
private User manager;
@Valid
private User operator;
答案 2 :(得分:1)
在您的控制器中,您可以添加自定义验证器:
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new ProjectValidator());
}
在此验证器中,您可以检查User
个对象或委派给UserValidator
,与第6.3节前一段中的here完成一样
答案 3 :(得分:0)
我做了Jerome Dalbert建议的内容,另外添加了一个自定义BeanValidator,用于将验证的实际工作委托给JSR 303实现。
前缀用于表示表单中属性的路径。
@Component
public class BeanValidator implements org.springframework.validation.Validator, InitializingBean {
private Validator validator;
public void afterPropertiesSet() throws Exception {
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
validator = validatorFactory.usingContext().getValidator();
}
public boolean supports(Class clazz) {
return true;
}
public void validate(Object target, Errors errors, String prefix) {
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(target);
for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
String propertyPath = constraintViolation.getPropertyPath().toString();
String message = constraintViolation.getMessage();
errors.rejectValue(prefix + "." + propertyPath, "", message);
}
}
public void validate(Object target, Errors errors) {
validate(target, errors, "");
}
}
以及我在UserValidator中如何使用它:
@Component
public class UserValidator implements Validator {
@Autowired
BeanValidator beanValidator;
@Override
public boolean supports(Class<?> clazz) {
return User.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
User user = (User) target;
beanValidator.validate(user.getAddress(), errors, "address");
}
}
参考文献: