[英]How to pass String collection from controller to view in Spring MVC
I'm trying to use "Tag-it" jQuery UI plugin with Spring MVC 我试图在Spring MVC中使用“ Tag-it” jQuery UI插件
My controller gets list of available tags from DAL and add it to model as attribute: 我的控制器从DAL获取可用标签列表,并将其添加到模型中作为属性:
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model){
DAL dal = new DAL();
model.addAttribute("FBAppId", ConfigurationManager.getInstance().getFBAppId());
model.addAttribute("FBAppSecret", ConfigurationManager.getInstance().getFBSecret());
model.addAttribute("tags", dal.getAllTags());
return "home";
}
Then I want to read it in view and pass it as list of available tags: 然后,我想在视图中阅读它,并将其作为可用标签的列表传递:
$(document).ready(function() {
$("#myTags").tagit({
availableTags: '${tags}'
});
$.ajaxSetup({ cache: true });
$.getScript('//connect.facebook.net/en_UK/all.js', function(){
FB.init({
appId: '${FBAppId}',
channelUrl: '//yourapp.com/channel.html',
});
$('#loginbutton,#feedbutton').removeAttr('disabled');
FB.getLoginStatus(updateStatusCallback);
FB.XFBML.parse();
});
});
However what interpreted is : "[tag1, tag2]" instead of ["tag1", "tag2"] 但是,解释为:“ [tag1,tag2]”而不是[“ tag1”,“ tag2”]
Does somebody know how to solve this issue? 有人知道如何解决这个问题吗?
${tags}
invokes the toString()
method of ArrayList
. ${tags}
调用ArrayList
的toString()
方法。 Instead, you need to prepare a comma-separated string. 相反,您需要准备一个以逗号分隔的字符串。 You can do that in the contoroller or in the view.
您可以在控制器或视图中执行此操作。
In the controller you can use a json mapper (eg jackson): 在控制器中,您可以使用json映射器(例如jackson):
ObjectMapper mapper = new ObjectMapper(); // this better be a field
String tags = mapper.writeValueAsString(getAllTags());
model.addAttribute("tags", tags);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.