0

The below code works fine when I try to save a single value of miniApple. I can see the value of miniApple object value in argument of controller i.e., apple. However I am not able to binder if there are multiple values. Not sure what should be the syntax to write inside the initBinder method for converting the string (comma separated value) into List of custom objects(here List of MiniApple)

When I select multiple values, I can the text value in setAsText() as comma separated values( eg: "miniApple1, miniApple2,miniAPlle3")

Controller method save()


public ModelAndView save(@ModelAttribute("Apple") Apple apple, BindingResult result, SessionStatus status) {

}

Init Binder method


@InitBinder
    public void initBinder(WebDataBinder dataBinder) {
        dataBinder.registerCustomEditor(MiniApple.class, new MiniAppleEditor(this.miniAppleService, true));
    }

Custom Editor class with setAsText method


public class MiniAppleEditor extends PropertyEditorSupport {

    /** The mini apple service. */
    private final MiniAppleService miniAppleService;

    /** Whether or not to allow null values. */
    private boolean allowEmpty;

    /**
     * @param miniAppleService  the miniAppleService to set
     */
    public MiniAppleEditor(MiniAppleService miniAppleService) {
        this(miniAppleService, false);
    }
    /**
     * @param miniAppleService  the miniAppleService to set
     * @param allowEmpty  indicates to allow empty  mini apple.
     */
    public MiniAppleEditor(MiniAppleService miniAppleService, boolean allowEmpty) {
        this.miniAppleService = miniAppleService;
        this.allowEmpty = allowEmpty;
    }

    /**
     * @param text  the id representing the MiniApple object.
     */
    @Override
    public void setAsText(String text) {
        if (allowEmpty && !StringUtils.hasLength(text)) {
            setValue(null);
        } else {
            setValue(miniAppleService.getMiniApple(Integer.parseInt(text)));
        }
    }

    /**
     * @return the Id as a String
     */
    @Override
    public String getAsText() {
        MiniApple miniApple = (MiniApple) getValue();
        if (miniApple  == null) {
            return "";
        } else {
            return miniApple.getAppleName();
        }
    }
}

Object which will get the values binded as an argument of controller save () method


@Entity
@Table("tbl_apple")
public class Apple implements Serializable {

private MiniApple miniApple;

getter/setter;

}
  • 1
    How should you be able to? Your `Apple` has a field of the type `MiniApple` it isn't a collection. So multiple selection won't work anyway. – M. Deinum Oct 01 '14 at 11:07
  • I will add List in the Apple class. what is the code/syntax required in initBinder method for converting string("miniApple1, miniApple2") into list of custom miniApple objects. – Kishore Paparthi Oct 01 '14 at 11:09
  • To put my question simple, if setAsText(String text) method returns List of customer Objects, the how will be the code in initBinder method should appear? – Kishore Paparthi Oct 01 '14 at 11:54
  • No it should return a single element. When there are multiple values the same parameter should be in the erquest multiple times and not as a comma separated value (if I recall correctly). How are you doing the binding on the client side? – M. Deinum Oct 01 '14 at 12:03
  • @ M.Deinum...My JSP code: – Kishore Paparthi Oct 01 '14 at 12:46
  • If user selects multiple values in drop down, then this will send a comma separated string ("miniApple1,miniApple2, miniApple3") to setAsText method – Kishore Paparthi Oct 01 '14 at 12:48
  • If your path type is a collection it shouldn't send a comma separated list to that method. It will only do this and call the method if it is a single element. Tip instead of the `forEach` use `form:options` instead. – M. Deinum Oct 01 '14 at 13:04

0 Answers0