Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

204
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda