gpt4 book ai didi

twitter-bootstrap - 带有 Spring MVC 的 X 可编辑 Select2 标签

转载 作者:行者123 更新时间:2023-12-04 04:32:35 24 4
gpt4 key购买 nike

我正在使用 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 Controller
    @RequestMapping(value="/updateForm", method = RequestMethod.POST)
public @ResponseBody String doUpdateForm(@ModelAttribute("object") MyObjectDTO object,
HttpServletRequest request) throws ParseException{
...
}

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

最佳答案

这些是我为使 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 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 可编辑 Select2 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20335380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com