Vicky Nandne Vicky Nandne - 1 month ago 7
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

addEmployee.jsp



<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>

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





Controller

@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());
List<State>list=iEmployeeService.getStateList();
model.addAttribute("states",list);

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);
List<District>list=iEmployeeService.getDistrictList();
return list;
}


Jquery function..

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
$('select#state').change(function() {
var MSG=$("#state").val();
alert(MSG);
$.post("employee?action=districtList", {
stateId: MSG
}
,
function(data) {
alert("sucess");
}
);


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

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

Try something like this :

$.ajax({
  type: "GET",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});