I've dynamically loaded a dropdown with ajax
call but the problem is I can't access it in controller
. the dynamically loaded dropdown is found as null
in controller!
JSP Code:
<body>
<c:url var="updateUrl" value="${actionPath}"/>
<form:form method="post" action="${updateUrl}" commandName="complaintDetail">
<table>
<tr>
<td><form:label path="complaintCategory">Category</form:label></td>
<td>
<form:select path="complaintCategory">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${categoryList}"/>
</form:select>
</td>
</tr>
<tr>
<td><form:label path="complaintReasonDetail">Reason</form:label></td>
<td>
<form:select path="complaintReasonDetail">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${reasonList}" />
</form:select>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="${submitString}">
</td>
</tr>
</table>
</form:form>
</body>
The JS Code:
$('#complaintCategory').change(
function(){
var findReasonsURL = '/Vela-web-client/complaint/getReason/' + $(this).val() + '/GENERAL.htm';
//alert($(this).val());
$.getJSON(findReasonsURL, {},
function(data) {
var html = '<option value="">--- Select ---</option>';
var len = data.length;
$.each(data, function(i, obj){
html += '<option value="' + obj.idNr + '">' + obj.complaintReasonSt + '</option>';
});
$('#complaintReasonDetail').html(html);
});
});
For this ajax call, the corresponding controller code:
@RequestMapping(value = "/complaint/getReason/{category}/{rType}", method = RequestMethod.GET)
public @ResponseBody
List<ComplaintReasonDetail> getReason(
@PathVariable(value = "category") String category,
@PathVariable(value = "rType") String rType)
{
List<ComplaintReasonDetail> complaintReasonDetails;
ComplaintCategory complaintCategory = ComplaintCategory.valueOf(category);
ReasonType reasonType = ReasonType.valueOf(rType);
try
{
complaintReasonDetails = ComplaintReasonHandler.getComplaintReasonAdvanceSearchResult("", false, complaintCategory, true, reasonType, true, BigInteger.ONE);
System.out.println("reason list count = " + complaintReasonDetails.size());
return complaintReasonDetails;
}
}
Up to this, everything works fine!
The problem occurs when I submit the form. I get complaintReasonDetail
as null
in controller code.
Controller Code:
@RequestMapping(value = "/complaint/doadd/addnew", method = RequestMethod.POST)
public String addCompliant(@RequestBody @ModelAttribute("complaintDetail") ComplaintDetail complaintDetail, BindingResult result, Model model)
{
System.out.println("category = "+complaintDetail.getComplaintCategory()); // OK
System.out.println("reason = "+complaintDetail.getComplaintReasonDetail()); // print null ???
}
It's a bit long question, but to make it clear, i think it is necessary.
So, what am I missing actually?