samedi 21 mars 2015

Spring and Velocity - form handling

I googled it, but could not find completely full answer. How can I handle the form from Spring, if I use Velocity templates as View? Assume I have the following form:



<form action="" method="POST" id="newThreadForm">
<input type="text" placeholder="Title" id="title"/>
<input type="text" placeholder="Author" id="author"/>
<input type="text" placeholder="E-mail" id="email">
<!-- other fields... -->
</form>


And have a simple class:



public class Post {
private int id;
private String author;
private String email;
private String title;
//other fields

//getters and setters
}


How can I convert the data that user types in the form to Post object in my Controller method?



@RequestMapping(value="path/to/", method=RequestMethod.POST)
public String newThread(Model model) {
//what should be there?
return "view-name";
}


Do I need to create a new class, smth like public class NewThreadForm? Add BindingResult argument to the method? Or what should I do? Thanks for answers.


Aucun commentaire:

Enregistrer un commentaire