twitter-bootstrap - 使用 Spring MVC X-editable Select2 标签

标签 twitter-bootstrap spring-mvc jquery-select2 x-editable

我正在使用 X-editable 插件来更新具有多个字段和数据类型的表单。表单的每个元素都有一个 name映射 DTO 内的 Java 属性的值。当使用 Ajax 提交表单时,所有值都与 Java 对象的相应字段相匹配除了 TAGS 数组,理论上应该与字符串列表匹配,但不知何故我得到了 NumberFormatException .

堆栈跟踪

[Request processing failed; nested exception is java.lang.NumberFormatException: For input string: ""] with root cause
java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:991)

选择2标签模式

$('.issue-tags').editable({
                pk: 22,
                name: 'tags',                
                placement: 'top',      
                mode: 'popup',  
                select2: {                                  
                    tags: ${tags},
                    tokenSeparators: [",", " "]
                },                 
                ajaxOptions: {
                    type: 'put'
                }
          }); 

“tags”属性从数据库加载值。

提交按钮

 $('#btn-submit').click(function() {                  

      $('.editable').editable('submit', {                      

           url: './updateForm.html', 
           ajaxOptions: {
               dataType: 'json'
           },                             
           success: function(data, config) {                                
               ...                          
           },
           error: function(errors) {
              ...
           }
       });                        

});

Java DTO

public class MyObjectDTO implements Serializable {

    private List<String> tags = new ArrayList<String>();
    ...
}

Spring MVC Controller

    @RequestMapping(value="/updateForm", method = RequestMethod.POST)
    public @ResponseBody String doUpdateForm(@ModelAttribute("object") MyObjectDTO object, 
    HttpServletRequest request) throws ParseException{
    ...
    }

如果没有标签字段,表单将正确地将数据提交给 Controller 。

最佳答案

这些是我为使 Select2(标签模式)组件与 Spring MVC 配合使用而所做的更改:

选择2标签模式(JavaScript)

$('#issue-tags').editable({
                pk: 22,
                name: 'tagsMap',                 
                placement: 'top',      
                mode: 'popup',                   
                emptytext: 'No hay etiquetas definidas',
                inputclass: 'input-large',
                select2: {              
                    tags: ${allTags},
                    tokenSeparators: [",", " "],
                    id: function (item) {
                        return item.text;
                    }
                },                  
                ajaxOptions: {
                    type: 'put'
                }   
          }); 

其中 tagsMap 是我的 DTO 类的一个对象,它在提交时保存标签:

选择 2 标记模式(HTML 输入)

<a id="issue-tags" href="#" data-type="select2">${tagsByObject}</a>

其中 tagsByObject 包含一串以逗号分隔的标签,Select2 使用它来显示我的对象的特定标签

Java DTO

public class MyObjectDTO implements Serializable {

    private List<String> tags = new ArrayList<String>();
    private Map<String, Object> tagsMap = new HashMap<String, Object>();
    ...
}

其中 allTags 是解析为字符串的 JSON 对象,它填充 Select2 组件的下拉菜单,显示数据库中保留的所有当前标签

Spring MVC Controller

@RequestMapping(value="/showPage", method = RequestMethod.GET)
    public String showPage(Model model,  HttpServletRequest request){
    ...
            List<String> myTags = myObjectDTO.getTags();
            String tagsByComma = StringUtils.EMPTY;
            String allTags = StringUtils.EMPTY;

            if(!myTags.isEmpty()){
                for(int i = 0; i < myTags.size(); i++){
                    tagsByComma += myTags.get(i) + ", ";
                }               
                tagsByComma = tagsByComma.substring(0, tagsByComma.length() -2);
            }

            List<String> dbTags = myService.getTags();
            JSONArray array = new JSONArray();
            for(String s : dbTags){
                JSONObject obj = new JSONObject();
                obj.put("id", dbTags.indexOf(s));
                obj.put("text", s);
                array.put(obj);
            }

            allTags = array.toString();


            model.addAttribute("tagsByObject", tagsByComma);
            model.addAttribute("allTags", allTags.length() == 0 ? "[{}]" : allTags);
    ...
}

提交功能保持不变。

关于twitter-bootstrap - 使用 Spring MVC X-editable Select2 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20335380/

相关文章:

java - Spring 没有匹配类型的 bean,预计至少有 1 个 bean

java - 如果语言环境无效,Spring mvc 从请求中获取默认语言环境

java - 从 session 属性创建下拉列表

javascript - 如何在 select2 上调用 "if no search result"?

javascript - Select2 initSelection 已弃用

html - 在图像上排列标题

javascript - 防止窗口向下滚动

javascript - 当有带有 CSS 的 Bootstrap 按钮时启用段落的背景颜色 <p>?

laravel - Laravel Livewire 上的 select2 不起作用

javascript - 如何一次只能打开 1 个 Accordion ?