Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC one input field form

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.

like image 819
B.Gen.Jack.O.Neill Avatar asked Dec 06 '22 13:12

B.Gen.Jack.O.Neill


1 Answers

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.

like image 69
jelies Avatar answered Dec 09 '22 03:12

jelies