what is the best way in Spring MVC to send to server only one field in form? I need to send select box value, but also I want the select-box to be pre-filled with right value.
Normally I would have some form backing object, and bind him to form, but when I have only one field to send, I don´t need any form backing object. But than I connot use form:form and form:select for binding becouse it requires field in form backing object to be specified.
Thanks.
In your jsp/view, use a 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 form submit is done:
@RequestMapping(method=RequestMethod.POST)
public String submitForm(@RequestParam String selected) {
// your code here!
return "nextView";
}
To prefill the select-box, you have to pass the value manually from controller to view, and finally select it with JSTL (or whatever you are using) / javascript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With