我对@InitBinder
Spring 验证有疑问。
首先,代码:
控制器:
@Controller
@RequestMapping("/manage")
public class QuestionManagementController {
...
@InitBinder
protected void initBinder(WebDataBinder binder) {
System.out.println("======"+binder.getObjectName());
binder.setValidator(new QuestionListValidator());
System.out.println("======"+binder.getObjectName());
}
...
@RequestMapping(value = "question/{unitid}", method = RequestMethod.GET)
public String getQuestionEditor(@SessionAttribute("userEntity")
UserEntity loggedUser, Model model, @PathVariable("unitid")
long unitId) {
QuestionUnit qu = questionUnitDao.getQuestionUnitById(
QuestionUnit.class, unitId);
QuestionList list = questionListDao.getParentQuestionList(qu);
if (!isOwner(loggedUser, list)) {
throw new Http404Exception("Nie znaleziono strony.");
}
else {
model.addAttribute("questionUnit", qu);
model.addAttribute("listid", list.getId());
model.addAttribute("formUrl", "/manage/question/" + qu.getId());
System.out.println("sdhfdsfihui");
return "/question/adder/"
+ questionAnnotationProcessor.getJSPName(qu.getClass());
}
}
现在,使用 println 的强大功能,当调用此方法时,我会得到类似的结果:
INFO : pl.meble.taboret.controller.QuestionManagementController - entering: initBinder
INFO : pl.meble.taboret.controller.QuestionManagementController - w/args:[org.springframework.web.servlet.mvc.method.annotation.ExtendedServletRequestDataBinder@1dc696e]
======unitid
======unitid
INFO : pl.meble.taboret.controller.QuestionManagementController - entering:getQuestionEditor
INFO : pl.meble.taboret.controller.QuestionManagementController - w/args: [user, {}, 1245184]
sdhfdsfihui
INFO : pl.meble.taboret.controller.QuestionManagementController - entering: initBinder
INFO : pl.meble.taboret.controller.QuestionManagementController - w/args: [org.springframework.web.servlet.mvc.method.annotation.ExtendedServletRequestDataBinder@404629]
======questionUnit
class pl.meble.taboret.question.OpenQuestion
INFO : pl.meble.taboret.controller.QuestionManagementController - entering: handleMyException
INFO : pl.meble.taboret.controller.QuestionManagementController - w/args: [java.lang.IllegalStateException: Invalid target for Validator [pl.meble.taboret.validator.question.QuestionListValidator@1be6a65]: pl.meble.taboret.question.OpenQuestion@1f9cb2c]
所以看起来 init binder 在之前调用 - 这是正常的,在方法return
语句之后。返回的字符串是 Apache Tiles 定义的名称。
同样奇怪的是,init binder被调用了questionUnit
,validator被设置了,然后就报错了。
列表验证器如下所示
@Component
public class QuestionListValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
System.out.println(clazz.toString());
return QuestionList.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmpty(errors, "name", "name.empty");
}
}
我们可以看到打印的类的名称。
我不知道为什么会这样,但我很确定这是@InitBinder
没有任何参数的错。
我阅读了关于这个注释value
参数的spring文档,这里是
The names of command/form attributes and/or request parameters that this init-binder method is supposed to apply to.
Default is to apply to all command/form attributes and all request parameters processed by the annotated handler class. Specifying model attribute names or request parameter names here restricts the init-binder method to those specific attributes/parameters, with different init-binder methods typically applying to different groups of attributes or parameters.
那么这是否意味着没有参数,验证器会验证传入的所有内容(请求参数)和传出的所有内容(命令/表单属性)?如果是这样,为什么没有使用userEntity
参数调用 init binder。以及为什么@InitBinder
在控制器方法返回String之后调用。