Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax pass a "Map" object to Spring MVC Controller
    primarykey
    data
    text
    <p>It seems like Spring MVC doesn't know how to map a javascript "map" to a Java map object</p> <p>In the web UI, say, foo.jsp, </p> <pre><code>&lt;script&gt; var myMap = {}; myMap["people"] = ["Alex","Bob","Charles","Dave"]; myMap["fruit"] = ["Apple","Orange"]; $.ajax({ type : "POST", url : "/myURL", data : "myMap=" + myMap, // I tried "myMap="+JSON.stringify(myMap), as well, it doesn't work neither success : function(response) { alert("Success! response = " + response); }, error : function(e) { alert("AJAX error"); } }); &lt;/script&gt; </code></pre> <p>On the server side, I have a data model class just to receive data from the Web UI</p> <pre><code>@Setter @Getter class Parameters { private Map&lt;String, List&lt;String&gt;&gt; myMap; //this is the java class I want to map the string to } </code></pre> <p>And in the controller,</p> <pre><code>@RequestMapping(value = "/myURL", method = RequestMethod.POST) @ResponseBody public List&lt;String&gt; fooControl(Parameters parameters ) { // do something with parameters ... } </code></pre> <p>The error I got on the server side is like,</p> <pre><code>[tomcat:launch] Aug 14, 2013 3:12:37 PM org.apache.catalina.core.StandardWrapperValve invoke [tomcat:launch] SEVERE: Servlet.service() for servlet dispatcher threw exception [tomcat:launch] org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors [tomcat:launch] Field error in object 'Parameters ' on field 'myMap': rejected value [{"people":["Alex","Bob","Charles","Dave"],"fruit": ["Apple","Orange"]}]; codes [typeMismatch.repairInfomationParametersExperimental.constraints,typeMismatch.constraints,typeMismatch.java.util.Map,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [repairInfomationParametersExperimental.constraints,constraints]; arguments []; default message [constraints]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Map' for property 'constraints'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Map] for property 'myMap': no matching editors or conversion strategy found] </code></pre> <p>I guess there is a way to tell Spring how to map that JSON format string a Java Map?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload