spring converter in initbinder

Solutions on MaxInterview for spring converter in initbinder by the best coders in the world

showing results for - "spring converter in initbinder"
Madyson
30 Aug 2017
1public class CategoryEditor extends PropertyEditorSupport {
2
3    // Converts a String to a Category (when submitting form)
4    @Override
5    public void setAsText(String text) {
6        Category c = new Category(text);
7        this.setValue(c);
8    }
9
10    // Converts a Category to a String (when displaying form)
11    @Override
12    public String getAsText() {
13        Category c = (Category) this.getValue();
14        return c.getName();
15    }
16
17}
18
19
20
21
22public class MyController {
23
24    @InitBinder
25    public void initBinder(WebDataBinder binder) {
26        binder.registerCustomEditor(Category.class, new CategoryEditor());
27    }
28
29    ...
30
31}
32