34113

How instantiate a concrete class in init binder?

Question:

I have to bind an abstract class to my controller request handler method.

Before I instantiate the concrete class with @ModelAttribute annotated method:

@ModelAttribute("obj") public AbstractObh getObj(final HttpServletRequest request){ return AbstractObj.getInstance(myType); }

But now I try to do that without the @ModelAttribute annotated method, beacause every call to controller trigger model attribute annotated method too.

So I try to get concrete class with InitBinder and a custom editor, but it doesn't work.

My Init Binder:

@InitBinder(value="obj") protected void initBinder(final WebDataBinder binder) { binder.registerCustomEditor(AbstractObj.class, new SchedaEditor()); }

And My Post Handler:

@RequestMapping(method = RequestMethod.POST) public String create(@Valid @ModelAttribute("obj") AbstractObj obj, final BindingResult bindingResult) { //my handler }

This is my ObjEditor:

@Override public void setAsText(final String text) throws IllegalArgumentException { try{ if(text == null){ setValue(new ConcreteObj()); }else{ setValue(objService.findById(Long.valueOf(text))); } }catch(Exception e) { setValue(null); } }

Answer1:

@ModelAttribute is just a command object. It is not intended to have variuos implementations depending on request parameters. WebDataBinder just influences how all parameters are being mapped to command fields. So - create simple command object with field of type AbstractObj .

public class CommandObject { private AbstractObj type; public void setType(AbstractObj type) { this.type = type; } }

Recommend

  • How instantiate a concrete class in init binder?
  • How to wrap the entity data and pass to the spring validator
  • convert bitset to int in c++
  • How to get list of running applications on remote computer using PowerShell
  • How to get list of running applications on remote computer using PowerShell
  • How can I bind a factory to a annotation-qualified injection point?
  • Logging @Controller Requests Spring Boot
  • User defined constructor for Fortran derived type instance
  • Spring 3 Collection Binding
  • Jquery multiply data in textbox array
  • javafx 8 dialog and concurrency
  • How do I embed a custom user-control in MS Word?
  • gulp.watch running same task multiple times when saving many files
  • 2-table interaction: insert, get result, insert
  • CKeditor stripping font tags instead of converting to span
  • How can I add div content to a new page in jspdf?
  • KnockoutObservableArray with typed elements in TypeScript
  • How to add a focus style to an editable ComboBox in WPF
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • NHibernate Validation Localization with S#arp Architecture
  • HTML download movie download link
  • Bug in WPF DataGrid
  • Redux, normalised entities and lodash merge
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • WPF Applying a trigger on binding failure
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • how does django model after text[] in postgresql [duplicate]
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Why do underscore prefixed variables exist?
  • How to load view controller without button in storyboard?