可以将WebDataBinder#getObjectName()返回null吗? [英] Can WebDataBinder#getObjectName() return null?
问题描述
我扩展了
public class RenamingProcessor extends ServletModelAttributeMethodProcessor {
@Override
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest nativeWebRequest) {
...
binder.getObjectName();
...
}
}
可以 binder.getObjectName()
返回null?
我试图研究源代码,但是我在javadoc中没有找到这个信息? / p>
I tried to research source but I didn't find this information in javadoc?
推荐答案
不,它不能返回null(在这个声明中,我认为spring框架不会破坏)。
No, it can't return null (in this statement i assume spring framework is not broken).
首先,WebDataBinder将始终有一个值。看看 ServletModelAttributeMethodProcessor实现bindRequestParameters 他们使用 WebDataBinder binder
原样:
First off, WebDataBinder will always have a value. Looking at the ServletModelAttributeMethodProcessor implementation of bindRequestParameters they use WebDataBinder binder
as is :
ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
//No null check between cast and usage
servletBinder.bind(servletRequest);
使用哪个构造函数构建您收到的WebDataBinder无关紧要。在 WebDataBinder :
It does not matter which constructor is used to build the WebDataBinder which you receive. Of the 2 available on WebDataBinder:
1. public WebDataBinder(Object target);
2. public WebDataBinder(Object target, String objectName);
第二个很明显会有一个objectName - 在这个意义上说,我不相信任何正确的程序员在spring上工作会使用null值来调用它。
The second is obvious will have an objectName - in the sense that i don't believe any sane programmer working on spring will call this with a null value.
第一个调用 DataBinder构造函数使用objectName(public DataBinder(Object target,String objectName))调用构造函数,除了使用默认的objectName:
The first one calls the DataBinder constructor which calls the constructor with the objectName ( public DataBinder(Object target, String objectName)), except with a default objectName:
this(target, DEFAULT_OBJECT_NAME);
这是
public static final String DEFAULT_OBJECT_NAME = "target";
objectName没有setter。一旦初始化,它将保持一个值。
There is no setter for the objectName. Once initialized, it will keep having a value.
Sidenote:目标属性( getTarget()
)可以具有空值,如果绑定器仅用于转换普通参数值。不知道这个tho的申请。
Sidenote: the target attribute (getTarget()
) can have a null value if the the binder is just used to convert a plain parameter value. Not sure about the application for this tho.
更新:这个答案清除了我的关于Spring的DataBinder如何用于转换普通参数值的问题。
这篇关于可以将WebDataBinder#getObjectName()返回null吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!