I'm trying to post a model that contains a Map to my controller, I have tried two ways:
First I'm just using regular tag, it reaches my controller with the right String key, but the Object of the Map gets as null
<c:forEach items="${productQuote.prodconTm}" var="productQuoteObjTM" varStatus="status">
<tr>
<td>${productQuoteObjTM.key}</td>
<td>
<input value="${productQuoteObjTM.value.qty}" name="prodconTm['${productQuoteObjTM.key}']${productQuoteObjTM.value}" id="inputQuote${status.index}"/>
</td>
<td>${productQuoteObjTM.value.totalPrice}</td>
</tr>
</c:forEach>
Also I tried to pass the Map using form tag instead, as shown in the line below:
but it doesn't reach my controller, I'm passing this form to an ajax function:
$(document).ready(function() {
$('#quoteForm').submit(
function(event) {
$.ajax({
url : $("#quoteForm").attr("action"),
data : $("#quoteForm").serialize(),
type : "POST",
success : function(response) {
$("#prodConfig").html(response);
},
error : function(xhr, status, error) {
alert("ERROR:"+xhr.responseText);
}});
return false;
});
});
this is the controller that is expecting the model:
@RequestMapping(value="/saveConfig",method = RequestMethod.POST)
public ModelAndView currentquote(@ModelAttribute("productQuote") ProductQuoteDTO productQuote) {
..
}
please let me know where I'm doing wrong. Thanks!
You should use JSP Forms instead of raw HTML forms, it will make your life a whole lot simpler.
I suppose productQuote.prodconTm is your Map:
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<form:form id="quoteForm" commandName="productQuote">
<c:forEach items="${productQuote.prodconTm}" var="entry">
<tr>
<td>${entry.key}</td>
<td>
<form:input path="prodconTm['${entry.key}'].qty" />
</td>
<td>${entry.value.totalPrice}</td>
</tr>
</c:forEach>
</form:form>
I am not certain if prodconTm['${entry.key}'].qty in the path will work.
Otherwise, you may have to adapt your Map value's DTO to a simple String or a primitive