2

Im new to Spring MVC and trying to achive something that seems really simple. However i cannot get it to work or find any relevant examples.

Using Spring MCV 3.1 with annotations. I have a form with only one select-list in it. When this form is submitted, i want to be able to have the id of the select-value submitted to my controller. Thats it!

I dont want to wrap this value in a Binding object, i'd just like to send it to the controller, preferably by get like this: http://www.mydomain.com/admin/products?marketId=id

My Controller looks like this:

@RequestMapping(value = "/admin/products", method = RequestMethod.GET)
public ModelAndView getProducts(@RequestParam("marketId") String marketId) {

    ModelMap model = new ModelMap();

    // Logic to find products by marketId is not shown
    // ...

    model.addAttribute("products", products);

    return new ModelAndView("products", model);
}

I have not been able to create a jsp that compiles yet, but this is my latest jsp snippet:

<form:form method="GET" action="/admin/products.htms" methodParam="marketId" >
    <form:select path="marketId" items="${marketList}" onchange="this.form.submit();"/>
</form:form>

If anyone could help or point out some relevant examples i would be very grateful! I have looked at a lot of examples using a binding object to wrap the form-data, but as you can see im looking for something a bit simpler.

Cheers!

1 Answer 1

5

If you don't need features such as object binding and error reporting, you can use plain HTML form instead of <form:form>:

<form method="GET" action="/admin/products.htms">
    <select name="marketId" onchange="this.form.submit();">
        <c:forEach var = "item" items="${marketList}">
            <option value = "${item}">${item}</option>
        </c:forEach>
    </select>
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

Ah! Ofcourse. Why did I forget plain old html... I just wanted to be fancy and use the spring-tags.

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.