Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
Post Map<String,Object> to Spring Controller using Ajax

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!

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!