Spring MVC single form input field

What is the best way in Spring MVC to send only one field on a form to the server? I need to send the value of the selection field, but also I want the selection field to be pre-populated with the correct value.

Usually I should have a form support object and attach it to the form, but when I have only one field to submit, I do not need a form support object. But than I use the form form: form and form: select for binding, because this requires a field in the form support object.

Thanks.

+4
source share
1 answer

In your jsp / view, use the classic html <form/> and <select/> :

 <form id="form" method="POST"> <select id="selected" name="selected"> <option value="1">First value</option> <option value="2">Second value</option> </select> </form> 

In your controller, this method will get the selected value when the form request is sent:

 @RequestMapping(method=RequestMethod.POST) public String submitForm(@RequestParam String selected) { // your code here! return "nextView"; } 

To fill in the selection field, you need to manually transfer the value from the controller to view it and finally select it using JSTL (or whatever you are using) / javascript.

+14
source

All Articles