[英]How to pass String collection from controller to view in Spring MVC
我试图在Spring MVC中使用“ Tag-it” jQuery UI插件
我的控制器从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";
}
然后,我想在视图中阅读它,并将其作为可用标签的列表传递:
$(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();
});
});
但是,解释为:“ [tag1,tag2]”而不是[“ tag1”,“ tag2”]
有人知道如何解决这个问题吗?
${tags}
调用ArrayList
的toString()
方法。 相反,您需要准备一个以逗号分隔的字符串。 您可以在控制器或视图中执行此操作。
在控制器中,您可以使用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.