How to bind a comma separated string to a Java collection (List) in Spring form?

I'm stuck in a situation where I've an input element in a JSP where user enters tags. E.g. java, foo, bar, anotherTag..etc

<c:url var="saveUrl" value="/create" />
    <form:form modelAttribute="myAttribute" method="POST"
        action="${saveUrl}">

        <form:input path="myTitle" />
        <form:textarea path="myPost" />

        <form:input type="text" id="tagInput"path="???" />

        <input type="submit" value="create" />
</form:form>

Now in my domain model corresponding to this input is a

private List<Tag> listOfTags

How to bind a csv to a List. If I enter listOfTags in the path(which is wrong for obvious reasons), I get incorrect binding exception. How do I convert(or bind) a csv to a List so that the Spring form is submitted properly and the listOfTags get the tags entered in the JSP. What is the best way to achieve it?

Please help.

Answers


I'm not sure but try this. Do a simple html input :

<input type="text" id="tagInput" name="myTags" />

And then in your controller do something like :

@RequestMapping(value="/create", method=RequestMethod.POST)
public void create(..., @ModelAttribute("myAttribute") MyClass myAttribute,
                    @RequestParam("myTags") String myTags, ...) {
    ...
    myAttribute.setListOfTags(Arrays.asList(myTags.split(",")));
    ...
}

Note : for more generic ways to bind and convert objects, you may want to take a look at PropertyEditors and Converters.


Need Your Help

Is it possible to run selenium scripts using google compute engine?

selenium selenium-webdriver google-compute-engine

I would like to run my selenium scripts on the cloud/online/remotely. I have been advised google compute engine could allow this.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.