Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling a checkbox form in a dialog with Spring MVC
    text
    copied!<p>There are plenty of tutorials out there showing how to build a form with a simple POJO model object and the "command" property on the Spring taglib.</p> <p>Examples: <a href="http://fruzenshtein.com/spring-mvc-form-checkboxes/" rel="nofollow">A tutorial</a></p> <p>I seem to thrive on making my life hard, though and have my form running in a jQuery dialog box...whose checkbox statuses are pre-processed with a javascript function. I'm doing this through AJAX calls in that javascript function.</p> <p>Sooo, my form init looks like this:</p> <pre><code>&lt;form:form action="javascript:associateArtifacts()"&gt; </code></pre> <p>This invoked my javascript method:</p> <pre><code>function associateArtifacts(){ /* JSONIFY the list of checked boxes and send that data to the server here in an AJAX call */ var artifacts = []; $('#artifacts_container input:checkbox').each( function(j,listitem) { artifacts.push(listitem.name); }); $.ajax({ type: "POST", url: "associateArtifacts.html", data:JSON.stringify(artifacts), success: function(html){ alert( "Submitted"); } }); return false; </code></pre> <p>}</p> <p>The point of this is building a dialog with a list of checkboxes that are checked based on DB data that the user can modify then save back to the server.</p> <p>My issue is, I'm getting the dialog up and populated with the checkboxes and loaded with the values, but I seem to have not thought it through as I have no idea how to intercept the data coming to the server as JSON and process it.</p> <p>In the past I've done this with URL'd parameters, but in this case is a variably-sized large string of JSON data. </p> <p>Am I going to be forced to define an object that just hold a single List in it for my Spring MVC container to assign this JSON data to? Seems kind of ridiculous...</p> <p>Thoughts? Suggestions? Harsh criticisms for going at this entirely wrong?</p> <p>Thanks for any of the above.</p> <h2>EDIT</h2> <p>Signature looks like this: </p> <pre><code>@RequestMapping(value = "/associateArtifacts.html", method = RequestMethod.POST, headers = {"content-type=application/json"}) public void associateArtifacts(@RequestBody List&lt;String&gt; checkboxData){ </code></pre> <p>Client from data in Chrome Tools looks like this:</p> <pre><code>["checkboxTitle1","checkboxTitle2","checkboxTitle3"]: </code></pre> <p>Which is produced by my client when I take my array of checkBox names and do this to it:</p> <pre><code>JSON.stringify(arrayOfNames) </code></pre> <p>Current error is:</p> <pre><code>HTTPStatus 415 - The server refused this request because the request entity is in a format not supported by the requested resource for the requested method. </code></pre> <h2>EDIT #2</h2> <p>It was the lack of contentType on my AJAX call, as you suggested. Thanks.</p>
 

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