Dubliner
Dubliner

Reputation: 71

Sending <option> value from JSP form to Controller Spring MVC

Here is my form in JSP file:

    <p class="lead">Check out the books in my store...</p>

    <form:form action="${pageContext.request.contextPath}/bookListByCategory"> 
    <select path="category">
        <option value="Programming">Programming</option>
        <option value="Fiction">Fiction</option>
        <option value="Biography">Biography</option>
        <option value="Travel">Travel</option>
    </select>
    <input type="submit" name="submit" value="submit" />
    </form:form>

The form is sent to the method:

@RequestMapping(value = "/bookListByCategory/{category}")
public String getBooksByCategory(@PathVariable String category, Model model) {
    List<Book> bookListByCategory = bookService.getBookListByCategory(category);
    model.addAttribute("books", bookListByCategory);
    model.addAttribute("category", category);
    return "bookList";
}

For some reason it doesn't work and I can see in browser the following:

enter image description here

When I click on "submit" it will display 404:

enter image description here

I can see that the value of selected option is not sent to the Controller, which is required to process the request. Any thoughts?

Upvotes: 2

Views: 2873

Answers (2)

Dubliner
Dubliner

Reputation: 71

The working form now:

<form action="${pageContext.request.contextPath}/bookListByCategory"> 
    <select name="category" onchange="this.form.submit();">
        <option value="all">-- category --</option>
        <option value="Programming">Programming</option>
        <option value="Fiction">Fiction</option>
        <option value="Biography">Biography</option>
        <option value="Travel">Travel</option>
    </select>
</form>

And also two methods in the controller:

@RequestMapping(value = "/bookListByCategory/{category}")
public String getBooksByCategory(@PathVariable String category, Model model) {
    List<Book> bookListByCategory = bookService.getBookListByCategory(category);
    model.addAttribute("books", bookListByCategory);
    model.addAttribute("category", category);
    return "bookList";
}

@RequestMapping(value = "/bookListByCategory")
public String getBooksByCategoryForm(@ModelAttribute("category") String category, 
        Model model) {
    if (category.equalsIgnoreCase("all")) {
        return "redirect:/bookList/";
    } else {
        return "redirect:/bookListByCategory/" + category;          
    }
}

All is working fine and producing the desired output.

enter image description here

Upvotes: 5

Navoneel Talukdar
Navoneel Talukdar

Reputation: 4598

I will give a simplified example on how to send option to mvc controller.

So in jsp say you have this -

Note that I used <form:select/>here instead of only select.

<form:form action="bookListByCategory" method="get">
  <form:select path="category">
    <form:option value="NONE"> --SELECT--</form:option>
    <form:options items="${listOfCategories}"></form:options>
  </form:select>
  ...
</form:form>

Now in the controller using RequestParam

@RequestMapping(value = "/bookListByCategory", method = RequestMethod.GET)
public String getBooksByCategory(@RequestParam String category, Model model)
    throws Exception {

       //rest of code ....

}

Upvotes: 1

Related Questions