@initbinder 不适用于特定的模型属性 [英] @initbinder not working for specific model attribute

查看:36
本文介绍了@initbinder 不适用于特定的模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 @valid 和 @initbinder 来验证传递给服务的数据,但我面临一个问题,即 @InitBinder 仅在全球范围内有效,即

I am using @valid and @initbinder for validation of the data being passed to the service but I am facing an issue that @InitBinder is working only globally,i.e.

@InitBinder // possible to leave off for global behavior
protected void initBinder(WebDataBinder binder){
    binder.setValidator(new LoginRequestValidator());
}

而不是像我有一个名为 LoginRequest 的模型对象那样的特定模型属性:

and not for a particular model attribute like I have an model object named LoginRequest :

@InitBinder("LoginRequest") // possible to leave off for global behavior
protected void initBinder(WebDataBinder binder){
    binder.setValidator(new LoginRequestValidator());
}

在这种情况下,根本没有调用验证器..这是正确的方法吗?还是我遗漏了什么?

In this case the validator is not being called at all.. is this the right way to do this? or am I missing something?

推荐答案

查看方法的签名,它必须是

Looking at the signature of your method it has to be

@InitBinder("loginRequest")

带有小号

@InitBinder 的值可以是以下之一

  • 模型属性的名称
  • 请求参数的名称
  • 如果以上都不适用,则可以使用类的名称,但以小写字母开头.这就是 Spring 向模型公开未命名属性的方式.
  • The name of a model attribute
  • The name of a request parameter
  • If none of the above apply, then the name of the class can be used, but starting with a small letter. That's how Spring exposes unnamed attributes to the model.

这篇关于@initbinder 不适用于特定的模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆