Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

202
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda