【问题标题】:X-editable Select2 Tags with Spring MVC带有 Spring MVC 的 X 可编辑 Select2 标签
【发布时间】:2013-12-02 18:37:25
【问题描述】:

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

Select2 标记模式

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

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

提交按钮

 $('#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 控制器

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

没有标签字段,表单将数据正确提交给控制器。

【问题讨论】:

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


    【解决方案1】:

    这些是我为使 Select2(标记模式)组件与 Spring MVC 一起工作所做的更改:

    Select2 标记模式 (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 控制器

    @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);
        ...
    }
    

    提交功能保持不变。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 2023-04-08
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多