I have an app that is a mix between a single-page Backbone.js app and a Struts2 server-generated web application. I am aware that this is not exactly standard practice, but it is not in my power to change the architecture.
The application maintains some Javascript models in the client and does stuff with them (what exactly is irrelevant here). In order to change the properties of those models, Struts2-based actions have to be called over XHRs with the models POSTed to the form action using $.ajax
with the JSON converted to application/x-www-form-urlencoded
by jquery. This will return a HTML fragment representing a form, which can then be filled out by the user and is then converted back into a model using
$('form').serializeArray()
and a custom transformation step. This has been working okay-ish so far, but now we have models that may contain arrays, for example like this one:
{
"name": "Jannik",
"contact_methods": [
{
"type": "mobile",
"phone_number": "+491...."
}, {
"type": "email",
"email": "jannik.jochem@..."
}
]
}
JQuery will convert this to form key-value-pairs like this:
name=Jannik
contact_methods[0][type]=mobile
contact_methods[0][phone_number]=+491....
contact_methods[1][type]=email
contact_methods[1][email]=jannik.jochem@...
I now want to be able to iterate the entries in contact_methods
using Struts' taglib or anything else that is available in JSP, but I cannot figure out how to do this. If this is impossible, changing the way the JSON object is converted to and from key/value-pairs is acceptable as well. Note however that the nested objects may have different field combinations, so just mapping the individual fields to array parameters would not work because the array indexes would get messed up.
Is there a way to accomplish this in Struts?
EDIT here's the code (note that this is a constructed example since I cannot post the actual code here due to confidentiality issues):
JSONTestAction
:
public class JSONTestAction extends ActionSupport {
}
jsontest.jsp
:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<b>Parameters:</b><br>
<pre>
<s:property value="#parameters"/>
</pre>
<b>Contact methods:</b><br>
<ol>
<s:iterator value="#parameters['contact_methods']">
<li><pre><s:property/></pre></li>
</s:iterator>
</ol>
Backbone view snippet:
var model = {
"name": "Jannik",
"contact_methods": [
{
"type": "mobile",
"phone_number": "+491...."
}, {
"type": "email",
"email": "jannik.jochem@..."
}
]
};
$.ajax({
url: contextRoot + '/properties_jsontest.action',
type: 'POST',
data: model,
success: function (data) {
var form = $('<form id="element-form" role="form"></form>');
form.appendTo(view.$el);
form.html(data);
}
});
The result is this:
Parameters:
{contact_methods[1][type]=[Ljava.lang.String;@5e2d7ab9, contact_methods[0][type]=[Ljava.lang.String;@7bd437fc, name=[Ljava.lang.String;@29a7c0e7, contact_methods[1][email]=[Ljava.lang.String;@4469930e, contact_methods[0][phone_number]=[Ljava.lang.String;@6f359f38}
Contact methods:
I want to iterate over contact_methods
inside the JSP.