Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use a non-bean style object as a model attribute for a spring form?

I am using a third party library that has objects that I want to be able to edit via a Spring (version 3.2) form. The problem is that the object does not use the Java-beans naming convention because the properties can be dynamic in some cases. Instead, the objects have methods like object.getAttr("attribute_name") and object.setAttr("attribute_name", value). It would be easy for me to write an implementation of BeanWrapper to wrap this type of object, because there are methods for querying what attributes are available, etc.

I also don't need custom editors because the properties are mostly built-in types. All I need is a way to customize the data binding or find some other way to plug in my custom BeanWrapper so that Spring's form tag will know how find the properties on the object when binding the form values. Seems like this should be fairly easy (that is why BeanWrapper is an interface, right?) but I am just not seeing it...

like image 805
matt forsythe Avatar asked Nov 28 '25 12:11

matt forsythe


1 Answers

For version 3.2 you could:

  • Extends BeanPropertyBindingResult and override createBeanWrapper() method.
  • Extends ExtendedServletRequestDataBinder and override getInternalBindingResult() method to return your bindingResult instance.
  • Extends ServletRequestDataBinderFactory and return your databinder instance.
  • Extends RequestMappingHandlerAdapter and override createDataBinder() method.
  • Register your handler adapter in dispatcher servlet context with order < Ordered.LOWEST_PRECEDENCE.

So I think that you need to extends four framework classes to pull up a custom BeanWrapper into Spring MVC.

Maybe there are simpler options.

like image 129
Jose Luis Martin Avatar answered Dec 01 '25 01:12

Jose Luis Martin