user2409128
user2409128

Reputation: 481

how to fetch data from dropdown into spring mvc controller

I am working on Spring mvc and also new in this concept. I have a dropdown using unordered list and I want to get the data from the dropdown in Controller when i click the submitt button. I also have text field, which is very easy to get the test field into contoller using. but i dont know how to get the data from the drop down list. my jsp page is like this I have implemented html code like this

<form role="form" method="post" action="/Web/password.html">
    <fieldset>
        <div class="form-group input-group">
            <span class="input-group-addon">
                <i class="glyphicon glyphicon-user"></i>
            </span> 
            <input class="form-control" placeholder="User Name" name="userName" type="email" required="" autofocus="">
        </div>

        <div class="form-group input-group">
            <span class="input-group-addon">Applications</i></span>
            <div class="btn-group" id='btnn'>
                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                    <span data-bind="label">Select One Application</span>&nbsp;<span class="caret"></span>
                </button>
                <ul class="dropdown-menu" name="dropDown" role="menu" style="height:200px;overflow: auto;" >
                    <c:forEach var ="entry" items="${listOfApp }">
                        <li><a tabindex="-1" href=""><c:out value="${entry }" /></a></li>
                    </c:forEach>
                </ul>
            </div>
        </div>

    </fieldset>
</form>

and my controller is

@RequestMapping(value = "/password.html", method = RequestMethod.POST)

    public String submit(@RequestParam (value ="userName") String userName,
            @RequestParam ("dropDown") String dropDown) {   

        System.out.println(dropDown+" "+userName);

        return "Hi";

    }

I have used <li> as text field in controller. but on server it is showing error that it not getting the value from name 'dropdown'. If someone knows how to solve this then please help me.

Upvotes: 0

Views: 2465

Answers (1)

Haim Raman
Haim Raman

Reputation: 12043

Spring MVC will populate the model from html select, but your drop down is based on ul/li which are not html inputs.

If you can switch to select or spring tasgs form:select, form:option or form:options it will work. You will need to use the taglib for spring tags, place it on the top of your jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

If you have to keep the ul/li you need to back it with an hidden field.

To use hidden remove the name dropDown from the ul. In javaScript attach an event listener on selection change and sync the ul/li with the hidden The hidden feild should have the name dropDown.

<input type="hidden" name="dropDown" />

Try the following link for hints http://www.mkyong.com/spring-mvc/spring-mvc-dropdown-box-example/

Spring related documentation http://docs.spring.io/spring/docs/current/spring-framework-reference/html/view.html

Upvotes: 1

Related Questions