Reputation: 9681
I'm posting a json request to server. I can see the http post is done correctly in firebug. (I'm sure the json format is valid)
eg. {datas:[{"a":1, "b":2},{"a":5, "b":6}]}
my question is, how do I recover this data in the server side? I read people saying to use request.getInputStream
but can't figure how to convert it into string or parse with GSON.
another question: is there a limite of size of a json string I can post?
(Struts 2)
dojo.xhrPost({
url: "sub/sub/theAction",
postData: dojo.toJson(json),
handleAs:'json',
headers: { "Content-Type": "application/json; charset=utf-8"},
load: function(data) {
alert("done");
}
}
});
thanks in advance
Upvotes: 0
Views: 2224
Reputation: 23587
i am not sure how DOJO works or post data but believe must be same as other javascript framework works. here is what we can do to achieve what you are trying to do
<package name="demojson" extends="struts-default,json-default">
<action name="jsonTest" class="com.demo.action.JsonTestAction">
<interceptor-ref name="json">
<param name="contentType">application/json</param>
</interceptor-ref>
<result type="json"/>
</action>
</package>
public class JsonTestAction extends ActionSupport
{
private static final long serialVersionUID = 1L;
private Map data = new HashMap();
// log decelaration
public Map getData()
{
return data;
}
public void setData(Map data)
{
this.data = data;
}
@Override
public String execute()
{
logger.log(Level.FINE, "action parameters: " + data);
return SUCCESS;
}
}
var submit = function()
{
var data = {data: {j1 : 'one', j2: 'two', j3: [1, 2, 3, 4, 5], j4: {a: 'A', b: 2, c: '3'}, j5: 2, j6: '2'}};
jQuery.ajax(
{
type: 'POST',
url: '/blah/jsonTest.action',
data: jQuery.toJSON(data),
dataType: 'json',
async: false ,
contentType: 'application/json; charset=utf-8',
success: do what you want here}
});
how json works with Struts2 refer this
hope this will help you
Upvotes: 1
Reputation: 160301
Use the JSON Plugin along with its interceptor; the Struts 2 action will be populated using the request data.
Note the requirements for the JSON on the plugin docs page.
Upvotes: 0