Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring/initBinder-选择多个值时无法获取绑定的值_Spring_Spring Mvc_Propertyeditor_Spring Mvc Initbinders - Fatal编程技术网

Spring/initBinder-选择多个值时无法获取绑定的值

Spring/initBinder-选择多个值时无法获取绑定的值,spring,spring-mvc,propertyeditor,spring-mvc-initbinders,Spring,Spring Mvc,Propertyeditor,Spring Mvc Initbinders,当我尝试保存miniApple的单个值时,下面的代码可以正常工作。我可以在控制器(即apple)的参数中看到miniApple对象值的值。但是,如果有多个值,我无法进行绑定。不确定在initBinder方法中应该写什么语法来将字符串(逗号分隔的值)转换为自定义对象列表(这里是MiniApple列表) 当我选择多个值时,我可以将setAsText()中的文本值作为逗号分隔的值(例如:“miniApple1、miniApple2、miniapple3”) 控制器方法save() 初始粘结剂法 使

当我尝试保存miniApple的单个值时,下面的代码可以正常工作。我可以在控制器(即apple)的参数中看到miniApple对象值的值。但是,如果有多个值,我无法进行绑定。不确定在initBinder方法中应该写什么语法来将字符串(逗号分隔的值)转换为自定义对象列表(这里是MiniApple列表)

当我选择多个值时,我可以将setAsText()中的文本值作为逗号分隔的值(例如:“miniApple1、miniApple2、miniapple3”)

控制器方法save()


初始粘结剂法


使用setAsText方法的自定义编辑器类


对象,该对象将获取绑定为controller save()方法参数的值



你应该怎么做?您的
Apple
有一个类型为
MiniApple
的字段,它不是集合。所以多重选择无论如何都不起作用。我将在Apple类中添加列表。initBinder方法将字符串(“miniApple1,miniApple2”)转换为自定义miniApple对象列表所需的代码/语法是什么。简单地说,如果setAsText(string text)方法返回客户对象列表,initBinder方法中的代码应该如何显示?不,它应该返回单个元素。当有多个值时,同一参数应该多次出现在erquest中,而不是以逗号分隔的值出现(如果我没有记错的话)。如何在客户端进行绑定?@M.Deinum…我的JSP代码:
public ModelAndView save(@ModelAttribute("Apple") Apple apple, BindingResult result, SessionStatus status) {

}
@InitBinder
    public void initBinder(WebDataBinder dataBinder) {
        dataBinder.registerCustomEditor(MiniApple.class, new MiniAppleEditor(this.miniAppleService, true));
    }
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();
        }
    }
}
@Entity
@Table("tbl_apple")
public class Apple implements Serializable {

private MiniApple miniApple;

getter/setter;

}