4

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.

1 Answer 1

15

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If CSFR protection is enabled in your context you need to include <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> in the form - the Spring form tag adds this automatically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.