简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM