Vicky Nandne Vicky Nandne - 1 year ago 84
jQuery Question

JQuery,Spring MVC Dynamically loading select options for city district on Registration form

I have a jsp form like this... Values in the second select box, District should be loaded based on value in first select box (state),. I am using jQuery, Spring MVC to retrieve results.but for that i first required value of stateId in my controller and this value not getting in my controller


<form:form id="employeeForm" modelAttribute="personaladdress" method="post" action="${baseURL}employee/save" class="form-horizontal" enctype="multipart/form-data">

<div class="form-group">
<label for="name" class="col-xs-4 control-label">state</label>
<div class="col-xs-8">
<form:select path="stateId" id="state">
<form:option value="" label="--Select State--"></form:option>
<c:forEach var="list" items="${states}">
<option value="${list.stateId }">${list.stateName }</option>

<form:errors path="stateId"></form:errors>
<form:select path="districtId" id="district">
<form:option value="" label="--select District--"></form:option>
<form:options items="${districtList}" itemValue="districtid" itemLabel="description"></form:options>


@RequestMapping(value="/addemployee",method = RequestMethod.GET)
public String addEmployee( @ModelAttribute("personalinformation") PersonalInformation personalinformation,
@ModelAttribute("personaladdress")PersonalAddress personaladdress,
@ModelAttribute("employee")Employee employee,BindingResult result,Model model){
model.addAttribute("personalinformation",new PersonalInformation());
model.addAttribute("employee",new Employee());
model.addAttribute("personaladdress",new PersonalAddress());

return "addEmployee";

@RequestMapping(value="/districtList",method = RequestMethod.GET)
@ResponseBody List<District> getDistrictList(@ModelAttribute("personalinformation") PersonalInformation personalinformation,
@RequestParam(value="stateId", required=true) String stateId, Model model){
System.out.println("inside districtlistst");
System.out.println("inside districtlist"+stateId);
return list;

Jquery function..

<script src=""></script>
$('select#state').change(function() {
var MSG=$("#state").val();
$.post("employee?action=districtList", {
stateId: MSG
function(data) {

i have registration form of employee in this i have to display Drop-down list of state,district and city dynamically.i have done with the Statelist but i am unable to get the id of selected state in my controller so then i can return a district list my form containing employee fields,personal information and personal address so that i have created two methods first returns string and displays registration page with state dropdown and another method for ditrict list but in jquery function i get the id but in getDistrictList method iam unable to get id.please suggest me what to do
Thanks in advance.

Answer Source

I see you are using post type ajax and your spring controller method accepts the get request.

Try something like this :

  type: "GET",
  url: url,
  data: data,
  success: success,
  dataType: dataType
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download