0

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.

2
  • does userName get populated? Commented Dec 8, 2014 at 5:13
  • yes. There is no error in userName Commented Dec 8, 2014 at 5:15

1 Answer 1

1

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

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

1 Comment

If want to change from Ul/li then so many thing i have to change in my jsp. Can you help me do it using hidden tag?

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.