如何发布对象的数组JSON Struts2的行动 [英] How to post an Array of object in json to Struts2 action
问题描述
我张贴JSON请求Struts2的应用。 JSON的请求有值的数组。这里是JSON reqeust:
<$p$p><$c$c>{\"row\":\"10\",\"col\":\"10\",\"data\":[{\"word\":\"word1\",\"clue\":\"clue1\"},{\"word\":\"word2\",\"clue\":\"clue2\"}]}JQuery的code:
jasonRequest = createpuzzle_createjson();$阿贾克斯({
输入:POST,
网址:'create.action',
数据类型:JSON,
数据:jasonRequest,
成功:功能(数据){
的console.log(字符串化(数据));
}
});
Action类:
公共类GenerateCWAction扩展ActionSupport的{私人串排;
私人字符串关口;
私人WCMap []的数据;公共字符串的getRow(){
返回行;
}
公共无效setRow(串行){
this.row =排;
}
公共字符串getCol(){
返回关口;
}
公共无效setCol(字符串COL){
this.col =关口;
}
公共WCMap []的getData(){
返回的数据;
}
公共无效使用setData(WCMap []数据){
this.data =数据;
}
公共字符串的execute(){
的System.out.println(的getRow:+的getRow());
的System.out.println(getCol:+ getCol());
的System.out.println(的getData:+的getData());
返回成功;
}
}
WCMap类:
公共类WCMap {
私人字符串字;
私人字符串的线索;
公共字符串屏幕取词(){
返回字;
}
公共无效setWord(字符串字){
this.word =字;
}
公共字符串getClue(){
返回的线索;
}
公共无效setClue(字符串线索){
this.clue =线索;
}
输出:
的getRow:10
getCol:10
的getData:空
我要检索的阵列数据
数据:[{词:字词1,线索:clue1},{字:字词2,线索:clue2}]
另外,我试图改变阵列下面列出;我仍然得到的getData:空
私人WCMap []的数据;
到
私人列表&LT; WCMap&GT;数据;
能否请你帮我摸不着头脑。
这答案是为未来的Google员工和我一样 -
1。创建一个拦截器堆栈
&LT;&拦截器GT;
&LT;拦截器堆栈NAME =jsonStack&GT;
&LT;拦截-REF NAME =json的&GT;
&LT; PARAM NAME =enableSMD&GT;真&LT; /参数&GT;
&LT; /拦截-REF&GT;
&LT; /拦截器堆栈&GT;
&LT; /拦截&GT;
2。使用这个拦截器为您的json行动
&lt;作用NAME =youraction级=类的方法=方法名&GT;
&LT;拦截-REF NAME =jsonStack&GT;&LT; /拦截-REF&GT;
&LT;结果类型=json的/&GT;
&LT; /作用&gt;
3。 Ajax调用
VAR ajaxData = {};
ajaxData [数组] = [//你的数据]。 //例[数据1,数据2];
$阿贾克斯({
数据类型:JSON,
类型:POST,
URL:'youraction.action',
数据:JSON.stringify(ajaxData)
的contentType:应用/ JSON的;字符集= UTF-8,
异步:假的,
成功:函数(JSON){
的console.log('成功:'+ JSON);
},
完成:功能(味精,A,B){
的console.log('完成:+味精);
},
错误:功能(味精,A,B){
的console.log('错误:'+味精);
}
});
4。在动作类的方法创建数组的getter和setter
列表&LT;串GT;阵列=新的ArrayList&LT;串GT;(); 公开名单&LT;串GT;的getArray(){
返回数组;
}
公共无效setArray(列表&LT;串GT;阵){
this.array =阵列;
}
I am posting JSON request to Struts2 application. The json request has array of values. Here is the JSON reqeust:
{"row":"10","col":"10","data":[{"word":"word1","clue":"clue1"},{"word":"word2","clue":"clue2"}]}
JQuery code :
jasonRequest = createpuzzle_createjson();
$.ajax({
type: 'POST',
url:'create.action',
dataType: 'json',
data: jasonRequest,
success: function(data){
console.log(stringify(data));
}
});
Action Class:
public class GenerateCWAction extends ActionSupport{
private String row;
private String col;
private WCMap[] data;
public String getRow() {
return row;
}
public void setRow(String row) {
this.row = row;
}
public String getCol() {
return col;
}
public void setCol(String col) {
this.col = col;
}
public WCMap[] getData() {
return data;
}
public void setData(WCMap[] data) {
this.data = data;
}
public String execute() {
System.out.println("getRow:" + getRow());
System.out.println("getCol:" + getCol());
System.out.println("getData:" + getData());
return SUCCESS;
}
}
WCMap class:
public class WCMap {
private String word;
private String clue;
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getClue() {
return clue;
}
public void setClue(String clue) {
this.clue = clue;
}
Output:
getRow:10
getCol:10
getData:null
I want to retrieve the array data
"data":[{"word":"word1","clue":"clue1"},{"word":"word2","clue":"clue2"}]
Also, I tried to change the array to list as below; Still i got getData:null
private WCMap[] data;
to
private List<WCMap> data;
Can you please help me to figure this out.
This answer is for future googler's like me -
1. Create a Interceptor stack
<interceptors>
<interceptor-stack name="jsonStack">
<interceptor-ref name="json">
<param name="enableSMD">true</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
2. Use this interceptor for your json action
<action name="youraction" class="your class" method="methodName">
<interceptor-ref name="jsonStack"></interceptor-ref>
<result type="json" />
</action>
3. Ajax call
var ajaxData = {};
ajaxData["array"] = [//your data]; // e.g ["data1","data2"];
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": 'youraction.action',
"data": JSON.stringify(ajaxData),
contentType: "application/json; charset=utf-8",
async : false,
success: function (json) {
console.log('success :'+json);
},
complete: function (msg,a,b) {
console.log('complete :'+msg);
},
error : function(msg,a,b){
console.log('error:'+msg);
}
} );
4. Create getters and setters for array in your action class method
List<String> array = new ArrayList<String>();
public List<String> getArray() {
return array;
}
public void setArray(List<String> array) {
this.array = array;
}
这篇关于如何发布对象的数组JSON Struts2的行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!