0

@InitBinderSpring の検証に問題があります。

まず、コード:

コントローラ:

@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 バインダーが前に呼び出されているように見えます-これは通常のメソッドreturnステートメントの後です。返される文字列は、Apache Tiles 定義の名前です。

questionUnitinit バインダーが で呼び出され、バリデーターが設定され、エラーが発生することも奇妙です。

リストバリデータは次のようになります

@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パラメーターに関する春のドキュメントを読みました。ここにあります

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バインダーの呼び出しがないのはなぜですか。@InitBinderそして、コントローラーメソッドが文字列を返した後に呼び出されるのはなぜですか。

4

1 に答える 1

1

パラメーターに @Valid アノテーションがない限り、または明示的に呼び出す場合を除き、バリデーターは呼び出されません。あなたの場合、彼らは呼び出されるべきではありません。ただし、ご覧のようにすべてのパラメーターに対して InitBinder メソッドが呼び出されます。

バリデーターがメソッドパラメーターに設定された時点で、バリデーターがパラメータータイプをサポートしているかどうかを判断するために、バリデーターの supports メソッドが呼び出されます。 @Validate アノテーションも持っていない限り

于 2012-09-27T19:25:09.623 に答える