[英]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.