1

I am working in spring mvc, I am doing some jsp with showing multiple dropdowns in a single pages....

I seen an example to show drop down from database by using the following example.

    <%@ page import="java.util.*" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <jsp:useBean id="state" scope="session" class="src.StateDAO"/>

    <html>
      <head>
      <title></title>
      </head>
      <body>
        <form id="test" method="POST" action="">
          <input name="state" type="radio" value="Australia" id="state-aus">Australia
          <input name="state" type="radio" value="NewZealand" id="state-new">NewZealand
          <input name="state" type="radio" value="India" id="state-oth"  >India
          <Select name="othStates" size="1" id="oth-states">
          <c:forEach items="${state.stateList}" var="st">
                <option value="1"><c:out value="${st.name}"/></option>
          </c:forEach>
          </select>
          <br>
          <input type="Submit" name="cmdSub" value="SUBMIT">
          <input type="Reset" name="cmdReset" value="RESET">
        </form>
      </body>
    </html>

Is this right way to do this to get dropdowns in jsp using Spring mvc?

1 Answer 1

5

I think that a better option is to use the spring tags for jsp

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
...
    <form:select path="country">
        <form:option value="NONE" label="--- Select ---" />
        <form:options items="${countryList}" />
    </form:select>

See full example here: http://www.mkyong.com/spring-mvc/spring-mvc-dropdown-box-example/

Edit:

$('#stateSelect').change(function() {
    $.ajax({
        type:"GET",
        url : "/getCitiesForState",
        data : { state: $('#stateSelect').val()},
        success : function(data) {
            $('#citySelect').empty(); //remove all child nodes
            for(var i = 0; i < data.length; i++){
                var newOption = $('<option value=data[i].value>data[i].text</option>');
                $('#citySelect').append(newOption);
            }   
        },
        error: function() {
            alert('Error occured');
        }
    });
});

On the server side you need an endpoint that respondes on the url(/getCitiesForState in the example) and returns a list of objects that have value and text properties.

Edit(add controlelr):

@Controller
public class HelloController{

   @RequestMapping("/getCitiesForState")
   @ResponseBody
   public List<City> printHello(@RequestParam long state) {
      List<City> cities = //get from the some repository by state
      return cities;
   }

}

public class City{
   private String value;
   private String text;
   //getters setters
}
Sign up to request clarification or add additional context in comments.

6 Comments

Also, I need to get the filtered dropdown for city based on the state dropdown. Can wwe have a way to do in spring tags. Or we need to do some other ways? I am very new to spring and j2ee..
This is usually done by an ajax call. You can submit the page on the State dropdown change, get the cities you want depending on the selected option and fill the model with the right cities. But this is really old school.
Is there any update in spring mvc, I searched but couldn't get clearance. example in spring or jsp would be helpful, if you can.
Example for the ajax way?
Yes, if you can please
|

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.