开发者

Spring MVC binding parameters to backing object

开发者 https://www.devze.com 2022-12-09 13:52 出处:网络
I am using Spring MVC and in my controller, I want to be able to automatically bind incoming parameters to my Java object. It seems like this should be pretty easy to do. The only wrinkle is that the

I am using Spring MVC and in my controller, I want to be able to automatically bind incoming parameters to my Java object. It seems like this should be pretty easy to do. The only wrinkle is that the incoming parameter names (e.g. "username") may not match up exactly with the field name in the java object (e.g. "name").

From the Spring documentation (http://static.springsource.org/spring/docs/2.5.6/reference/mvc.html):

"Spring Web MVC allows you to use any object as a command or form object.... All this means that you don't need to duplicate your business objects' properties as simple, untyped strings in your form objects just to be able to handle invalid submissions, or to convert the Strings properly. Instead, it is often preferable to bind directly to your business objects. "

How do I actually do this? Any code or links appreciated.

For example, my business object

public class User {
  private String username;
  private String password;

  //getters and setter
}

The request my controller is handling:

example.开发者_运维百科com/login?name=Steve&pw=1234

I would like to bind "Steve" to User.username and "1234" to User.password.

Thanks.


If I remember correctly, you can override public Object formBackingObject(HttpServletRequest request) and manually setup your command POJO.


In this case you can use custom argument resolver.

public class UserArgumentResolver implements HandlerMethodArgumentResolver {
    public Object resolveArgument(final MethodParameter parameter,
                                  final ModelAndViewContainer container,
                                  final NativeWebRequest webRequest,
                                  final WebDataBinderFactory binderFactory) {
        final User user = new User();
        user.setUsername(webRequest.getParameter("name"));
        user.setPassword(webRequest.getParameter("pw"));

        return user;
    }
}

And in your spring config:

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="your.package.UserArgumentResolver"/>
    </mvc:argument-resolvers>
</mvc:annotation-driven>


example.com/login?username=Steven&password=1234

You can't expect spring MVC to do automatic binding when the property names don't match up. That's just not logical.


EDIT: Do what Kaleb said, overriding formBackingObject is cleaner.

I'll leave this note though:

However, if you have any control over both sides, I strongly recommend making the name consistent. Convention over configuration.


If you want to bind request to the POJO you may need to extend AbstractCommandController or if you have a form - SimpleFormController. I believe you will need the servlet version from org.springframework.web.servlet.mvc package.

  • use setCommandClass() to setup the correct type of your backing bean in controller constructor, ie POJO:

    setCommandClass(User.class);

  • formBackingBean() used to create new instance of the POJO, that would be used by controller.

  • controller is responsible for mapping request parameters to the POJO fields.
  • if mapping is not working good for you, than override formBackingBean() or onBind() methods to read request paratemers and put values to POJO.


You should change your form or your object so that the names match up. If you're trying to use the features in a framework like Spring MVC, you should probably follow their conventions.

Convention over configuration.

0

精彩评论

暂无评论...
验证码 换一张
取 消