質問

I'm trying to use "Tag-it" jQuery UI plugin with Spring MVC

My controller gets list of available tags from DAL and add it to model as attribute:

@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"]

Does somebody know how to solve this issue?

役に立ちましたか?

解決

${tags} invokes the toString() method of ArrayList. 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 (e.g. jackson):

ObjectMapper mapper = new ObjectMapper(); // this better be a field
String tags = mapper.writeValueAsString(getAllTags());
model.addAttribute("tags", tags);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top