這聽起來像你想要一個實現org.springframework.validation.Validator
的自定義驗證類。
@Component
public class MyValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return MyBean.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
MyBean myBean = (MyBean) target;
if (StringUtils.isBlank(myBean.getProperty("state"))) {
errors.rejectValue("state", "blank");
}
}
}
在你的控制,你會做手工validaton像如下:
@Autowired
private MyValidator myValidator;
@RequestMapping(value = "save", method = RequestMethod.POST)
public String save(@ModelAttribute("myBean") MyBean myBean, BindingResult result) {
myValidator.validate(myBean, result);
if (result.hasErrors()) {
...
}
...
}
恕我直言,這個類是不豆。 – SJuan76 2012-04-23 21:41:08