Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation springmvc-initbinder on方法返回_Validation_Spring Mvc - Fatal编程技术网

Validation springmvc-initbinder on方法返回

Validation springmvc-initbinder on方法返回,validation,spring-mvc,Validation,Spring Mvc,我对@InitBinder和Spring验证有问题 首先,守则: 控制器: @Controller @RequestMapping("/manage") public class QuestionManagementController { ... @InitBinder protected void initBinder(WebDataBinder binder) { System.out.println("======"+binder.getObjectNam

我对
@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定义的名称

同样奇怪的是,使用
questionUnit
调用init binder,设置了验证器,然后出现了一个错误

列表验证程序如下所示

@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");
    }
}

那么,这是否意味着没有参数,验证器会验证传入的所有内容(请求参数)和传出的所有内容(命令/表单属性)?如果是这样,为什么不使用
userEntity
参数调用init binder。以及为什么在控制器方法返回字符串后调用
@InitBinder

除非您的参数具有@Valid注释,或者您显式调用它,否则不会调用验证器。在你的情况下,不应该给他们打电话。但是,InitBinder方法将为您看到的每个参数调用


在为方法参数设置验证器时,将调用验证器的supports方法以确定验证器是否支持该参数类型,也就是说,您可能看到正在调用支持,但是,除非您也有@validate注释,否则不会调用validate方法。在阅读了您的响应并对该注释进行了一些修改之后,我现在知道它是如何工作的了,谢谢。
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.