Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

Monday, February 10, 2014

Spring MVC 3: Property referenced in indexed property path is neither an array nor a List nor a Map

JQuery's $.ajax does an excellent job mapping a json object to parameters, but when you start getting into more complex objects Spring MVC doesn't know how to interpret it sometimes.

For example if you have a json object like:

{
   "beans":[
      {
         "agreemendId":1,
         "answerId":2
      }
   ]
}
JQuery will map your parameters like

beans[0][agreementId]:1
beans[0][answerId]:2

The problem is that Spring MVC is expecting a parameter format like

beans[0].agreementId:1
beans[0].answerId:2

In order to get that you can do it in 1 of two ways. You can do the quick and dirty way, which changes the way you're building your json object. Or, the other way is to extend the jQuery plugin to build parameters differently.

To change the javascript code was pretty simple and looked something like this

var answers = {};
answers['beans[' + index +'].agreementId'] = agreementId;
answers['beans[' + index +'].answerId'] = value;

To modify the jquery plugin I would suggest taking a look here.

And for reference here are the pojos I was mapping to.

Wednesday, September 4, 2013

Load A Javascript File Only If It Hasn't Been Loaded

Here's a neat little Javascript trick if you're worried about a JS resource being loaded more than once. In this example it checks if google maps have already been loaded.


You can also use this trick with !window.jQuery instead of !window.google.

Tuesday, January 8, 2013

Closing the JQuery UI Dialog by the UI Generated Class

The JQuery UI Dialog box creates a bunch of classes to surround the div you coded to act as the modal window.  One of these classes is the ui-dialog-content class. The neat thing about this class is that it is responds to the dialog methods, so if you want to close it and for whatever reason you don't know what div/element you might be in (separate template file perhaps), you can call:

$(".ui-dialog-content").dialog("close");

More info is available here.