此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10! |
DataBinder
@Controller
或@ControllerAdvice
类可以有@InitBinder
方法设置为 初始化WebDataBinder
反过来,这些实例可以:
-
将请求参数绑定到模型对象。
-
将请求值从字符串转换为对象属性类型。
-
在呈现 HTML 表单时将模型对象属性格式化为字符串。
在@Controller
,DataBinder
自定义在控制器内本地应用,甚至通过注释到名称引用的特定模型属性。在@ControllerAdvice
自定义可以应用于所有或一部分控制器。
您可以注册PropertyEditor
,Converter
和Formatter
组件中的DataBinder
用于类型转换。或者,您可以使用 WebFlux 配置进行注册Converter
和Formatter
全局共享的组件FormattingConversionService
.
-
Java
-
Kotlin
@Controller
public class FormController {
@InitBinder (1)
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
// ...
}
1 | 使用@InitBinder 注解。 |
@Controller
class FormController {
@InitBinder (1)
fun initBinder(binder: WebDataBinder) {
val dateFormat = SimpleDateFormat("yyyy-MM-dd")
dateFormat.isLenient = false
binder.registerCustomEditor(Date::class.java, CustomDateEditor(dateFormat, false))
}
// ...
}
1 | 使用@InitBinder 注解。 |
或者,当使用Formatter
通过共享的基于设置FormattingConversionService
,您可以重复使用相同的方法并注册特定于控制器Formatter
实例,如以下示例所示:
-
Java
-
Kotlin
@Controller
public class FormController {
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd")); (1)
}
// ...
}
1 | 添加自定义格式化程序(DateFormatter ,在本例中)。 |
@Controller
class FormController {
@InitBinder
fun initBinder(binder: WebDataBinder) {
binder.addCustomFormatter(DateFormatter("yyyy-MM-dd")) (1)
}
// ...
}
1 | 添加自定义格式化程序(DateFormatter ,在本例中)。 |
模型设计
Web 请求的数据绑定涉及将请求参数绑定到模型对象。默认情况下,请求参数可以绑定到模型对象的任何公共属性,这意味着恶意客户端可以提供模型对象图中存在的属性的额外值,但预计不会被设置。这就是为什么模型对象设计需要仔细考虑的原因。
模型对象及其嵌套对象图有时也称为命令对象、表单支持对象或 POJO(普通旧 Java 对象)。 |
一个好的做法是使用专用的模型对象,而不是公开您的域模型,例如 JPA 或 Hibernate 实体,用于 Web 数据绑定。例如,在表单上更改电子邮件地址,创建一个ChangeEmailForm
model 对象,该对象仅声明输入所需的属性:
public class ChangeEmailForm {
private String oldEmailAddress;
private String newEmailAddress;
public void setOldEmailAddress(String oldEmailAddress) {
this.oldEmailAddress = oldEmailAddress;
}
public String getOldEmailAddress() {
return this.oldEmailAddress;
}
public void setNewEmailAddress(String newEmailAddress) {
this.newEmailAddress = newEmailAddress;
}
public String getNewEmailAddress() {
return this.newEmailAddress;
}
}
另一个好的做法是应用构造函数绑定,它仅使用构造函数参数所需的请求参数,而任何其他input 都会被忽略。这与属性绑定形成鲜明对比,属性绑定默认绑定每个请求参数,其参数具有匹配的属性。
如果专用模型对象和构造函数绑定都不够,并且您必须使用属性绑定,我们强烈建议注册allowedFields
模式(大小写敏感)打开WebDataBinder
以防止设置意外属性。 例如:
@Controller
public class ChangeEmailController {
@InitBinder
void initBinder(WebDataBinder binder) {
binder.setAllowedFields("oldEmailAddress", "newEmailAddress");
}
// @RequestMapping methods, etc.
}
您也可以注册disallowedFields
模式(不区分大小写)。 然而 “允许”配置优于“不允许”配置,因为它更明确、更少容易出错。
默认情况下,同时使用构造函数和属性绑定。如果只想使用构造函数绑定,可以将declarativeBinding
标记 onWebDataBinder
通过@InitBinder
方法在控制器内本地或通过@ControllerAdvice
. 打开此标志可确保仅使用构造函数绑定并且不使用属性绑定,除非allowedFields
配置了模式。 例如:
@Controller
public class MyController {
@InitBinder
void initBinder(WebDataBinder binder) {
binder.setDeclarativeBinding(true);
}
// @RequestMapping methods, etc.
}