2

我正在使用 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)

Select2 标记模式

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

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

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

4

1 回答 1

3

这些是我为使 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>

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

Java DTO

public class MyObjectDTO implements Serializable {

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

解析为 StringallTags的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);
    ...
}

提交功能保持不变。

于 2014-01-28T15:58:29.340 回答