0

I've got a DTO (bean) with ArrayList field:

public MyDTO {
  ...
  private List<MyThing> things;
  ...
  ... getters, setters and so on
}

In my initBinder I have:

@InitBinder
public void initBinder(WebDataBinder binder) {
  ...
  binder.registerCustomEditor(List.class, "things", new PropertyEditorSupport() {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
       List<MyThing> things = new ArrayList<MyThings>;

       // fill things array with data from text
       ...


       // On that stage things value is correct!
       super.setValue(things);
    }
  });
}

And in my controller request method:

@RequestMapping({"save"})
public ModelAndView doSaveMyDTO(@ModelAttribute MyDTO myDTO) {
  // very strange myDTO comes here=(
}

The problem is that while I'm in registerCustomEditor staff the things array is ok.

But when I get to the doSaveMyDTO method - MyDTO.things looks like Array of one element arrays of actual values:

Expected (things in initBinder):

[value1, value2, value3]

Get in doSaveMyDTO (myDTO.getThings()):

[[value1], [value2], [value3]]

Why? Please explain...

1 Answer 1

2

If the request is correctly formed (things=v1&things=v2&things=v3 or things=v1,v2,v3), spring's built-in converters should properly convert it to a List - no need to register your own.

If your input is JSON, then you'd need @RequestBody instead of @ModelAttribute

Sign up to request clarification or add additional context in comments.

2 Comments

unfortunately I've got my list as a json string.. Anyway, thanks for your advice - I'll try to make it in your way...
ah, json is another thing. How does your json look like? And for it I think you many need @RequestBody

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.