简体   繁体   English

如何在Spring MVC中将字符串集合从控制器传递到视图

[英]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}调用ArrayListtoString()方法。 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.

相关问题 如何在 Spring-MVC 中将数据从视图传递到控制器? - How to pass data to from view to controller in Spring-MVC? 如何通过 <Select>和<input>数据从视图到控制器(Spring MVC) - How to Pass <Select> and <input> data from View to a Controller (Spring MVC) 如何在Spring MVC中将数组变量从视图传递到控制器 - How to pass array variable from view to controller in spring mvc 如何将列表从视图传递到 Spring MVC 和 thymeleaf 中的 controller? - How to pass a list from the view to the controller in Spring MVC with thymeleaf? MVC-如何将数据从视图传递到控制器 - MVC - How to pass data from view to controller 如何将参数从视图传递到Spring中的controller 3 - how to pass parameters from view to controller in Spring 3 如何使用Spring MVC将视图中的参数(标识对象的入门键)传递给控制器​​? - How to pass a parameter (that identify the primery key of an object) from the view to a controller using Spring MVC? 如何在Spring MVC中将LIST从一个控制器传递到另一个控制器 - How to pass LIST from one controller to another controller in spring mvc 如何从控制器传递json字符串以查看 - how to pass the json string from controller to view Spring MVC传递模型以查看然后传递给控制器 - Spring MVC pass model to view then to controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM