Dattatreya
Dattatreya

Reputation: 21

How to pass the drodown selected value in thymeleaf to spring boot controller

Please help me with any working code.I am trying from since one week not getting the solution.

Upvotes: 1

Views: 8572

Answers (3)

sendon1982
sendon1982

Reputation: 11244

<form th:action="@{/search}" th:object="${searchRequest}" method="post">    
    <select th:field="*{parkType}">
        <option value="1">Test1</option>
        <option value="2">Test2</option>
    </select>           
</form>

@RequestMapping(value = "/search", method = RequestMethod.POST)
public String searchParks(@ModelAttribute(value = "searchRequest") SearchRequest searchRequest, Model model) {
   
    searchRequest.getParkType(); // to get option value

    return "listings";
}

@lombok.Data
public class SearchRequest {
   private String name;
   private String parkType;
}

Upvotes: -1

Silo2017
Silo2017

Reputation: 19

Java MVC frameworks encapsulate Servlet APIs and common functionality is provided to us using the framework's simpler APIs.

For a task as common as retrieving a parameter value from a request, Spring MVC and Boot use the @RequestParam annotation to retrieve the parameter value selected from a select with option tag. So ak38's code would not need to use HttpServletRequest at all to get the parameter; instead, it would just annotate a paramter of the controller method the request calls:

HTML Form:

<form role="form" id="sendAddress" th:action=@{/sendAddress} method="post">
    <select class="form-control" name="*nameOfCity*">
        <option value="">Select City</option>
        <option value="HYD">Hyderabad</option>
        <option value="MUM">Mumbai</option>
        <option value="DEL">Delhi</option>
    </select>
</form>

Controller:

@RequestMapping(value={"/sendAddress"}, method = RequestMethod.POST)
public String messageCenterHome(**@RequestParam** String *nameOfCity*) {
    // value of nameOfCity is now value of "nameOfCity" paramter,
    // that is, the value of the option tag selected
    // return view 
}

Try it out, if you haven't already, as I've just seen this now and it's been a few months since the question was posted.

Upvotes: 1

Dil1y_reddy
Dil1y_reddy

Reputation: 697

Give name attribute to the html select element, you can access the selected value of drop down in the controller from HttpServletRequest object as below.

  <form role="form" id="sendAddress" th:action=@{/sendAddress} method="post">
        <select class="form-control" name="nameOfCity">
            <option value="">Select City</option>
            <option value="HYD">Hyderabad</option>
            <option value="MUM">Mumbai</option>
            <option value="DEL">Delhi</option>
        </select>
    </form>



    @RequestMapping(value={"/sendAddress"},method = RequestMethod.POST)
    public String messageCenterHome(Model model,HttpSession session,HttpServletRequest request) {

        String selectedCity= request.getParameter("nameOfCity")
        //return view 
    }

Upvotes: 6

Related Questions