Results 1 to 4 of 4

Thread: Map javascript array to controller object

  1. #1
    Join Date
    Jan 2012
    Posts
    21

    Default Map javascript array to controller object

    I'm pasing a JavaScipt array by POST with this code
    Code:
    var rowList = .. some code to fill the array;
    jQuery.post("?save", {rows: rowList },function(data){});
    It generate the following post info
    rows%5B0%5D%5Bid%5D=E202&rows%5B0%5D%5Bvalue%5D=Tr ue&rows%5B1%5D%5Bid%5D=E204&rows%5B1%5D%5Bvalue%5D =True

    The representation on Firefox findbugs...

    rows[0][id] E202
    rows[0][value] True
    rows[1][id] E204
    rows[1][value] True

    In order to get it on the controller, I have the one class with getters and setters

    Code:
        public class MyClass {
        	private String id;
        	private String value;
    ...
    }
    And inside the controller

    Code:
        @RequestMapping(value = "/{id}", params = "save", method = RequestMethod.POST, produces = "text/html")
        public String save(@PathVariable("id") Long id,
        		@RequestParam(value="rows[]", required=false) MyClass[] rowList, 
        		HttpServletRequest httpServletRequest){
    But rowList is null....

    What is the correctly way of handle this javascript arrays?

  2. #2
    Join Date
    Mar 2007
    Posts
    128

    Default

    I don't know if this will be any help since it's not quite the same as what you're doing, but this is working for me.

    In my case I'm doing it as an ajax call, and I'm posting the array (and nothing else) using JSON.stringify(). Not sure how you would modify this if you're passing additional fields.

    client side JavaScript:
    Code:
    var serverData = [];
    // ... populate serverData
    $.ajax({
    	  type: 'POST',
    	  url: sPostUrl,
    	  data: JSON.stringify(serverData),
    	  success: function(..) {
    		  ...
    	  },
    	  error: function(...) {
    		  ...
    	  },    			
    	  contentType: "application/json"
    server side Java:
    Code:
    @RequestMapping(value = "yourUrlHere", method = RequestMethod.POST)
    @ResponseBody
    public String yourMethodName(HttpSession session, @RequestBody PostedClass[] postedItems) {
    	  ...
    }
    Also, I don't like using "YourClass[]". I thought I should be able to do it as a List<PostedClass>, but I was getting errors doing it that way.

    Edit:
    I forgot - I believe to be able to handle the JSON as input I had to add a AnnotationMethodHandlerAdapter to my servlet config. The default Spring 3.1 one did not include org.springframework.http.converter.json.MappingJac ksonHttpMessageConverter. The first 5 message converters are what Spring uses by default (see here). I had to add the MappingJacksonHttpMessageConverter.

    Code:
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    	<property name="order" value="1" />
    	<property name="messageConverters">
    		<list>
    			<!-- Message converters -->
    			<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
    			<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
    			<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
    			<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
    			<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
    			<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
    		</list>
    	</property>
    </bean>
    Last edited by sbirnie; Jan 24th, 2013 at 11:26 AM. Reason: remembered having to add AnnotationMethodHandlerAdapter

  3. #3
    Join Date
    Jan 2012
    Posts
    21

    Default

    I'm very confused, posting the data as json also fails

    Client side:

    Code:
    jQuery.ajax(
    {
        url: "?save",
        type: "POST",
        success: alert("ok"),
        data:  JSON.stringify({rows: rowList }),
        contentType: "application/json"
    });
    I see de json data on findbugs and looks correct.
    {"rows":[{"id":"E202","value":"True"},{"id":"E204","value": "True"}]}

    Server side:

    Code:
        @RequestMapping(value = "/{id}", params = "save", method = RequestMethod.POST, produces = "text/html")
        public String save(@PathVariable("id") Long id,
        		@RequestBody(required=false) MyClass[] rows,
        		HttpServletRequest httpServletRequest){
    The code more or less the same as
    http://blog.springsource.com/2010/01...in-spring-3-0/

    But still not working, now I obtain 400 Bad Request, but without any error in the stacktrace.

  4. #4
    Join Date
    Jan 2012
    Posts
    21

    Default

    After change the log4j to debug I can see the error

    My controller class was:

    Code:
    public class MyController {
          public class MyClass {
        	private String id;
        	private String value;
        	}
    ...
        @RequestMapping(value = "/{id}", params = "save", method = RequestMethod.POST, produces = "text/html")
        public String save(@PathVariable("id") Long id,
        		@RequestBody(required=false) MyClass[] rows,
        		HttpServletRequest httpServletRequest){
    Using subclass it's a bad idea, I have moved MyClass to a separate file and now works ok.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •