Can WebDataBinder#getObjectName() return null?

2019-08-11 05:07发布

I extends

public class RenamingProcessor extends ServletModelAttributeMethodProcessor {
        @Override
        protected void bindRequestParameters(WebDataBinder binder,     NativeWebRequest nativeWebRequest) {
            ...    
            binder.getObjectName();
            ...
        }
}

Can binder.getObjectName() returns null?

I tried to research source but I didn't find this information in javadoc?

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-11 05:50

No, it can't return null (in this statement i assume spring framework is not broken).

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);

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);

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.

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);

which is

public static final String DEFAULT_OBJECT_NAME = "target";

There is no setter for the objectName. Once initialized, it will keep having a value.

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.


Update: This answer cleared up my question on How is Spring's DataBinder used to convert a plain parameter value.

查看更多
登录 后发表回答