我正在使用struts2。 当我从Js发送一个JsonObject到action时,JsonObject为空。
代码:
Js文件:
var data = {"data":[{"name":"ABC","age":"20"},{"name":"XYZ","age":"22"}]};
$.ajax({
url: "Exam1/start",
data: data,
dataType: 'json',
type: 'POST',
contentType: 'application/json',
success: function (res) {
alert("OK");
}
});
和struts.xml:
<package name="default" extends="json-default" namespace="/">
<action name="start" method="init" class="ta.action.InitAction">
<param name="enableSMD">true</param>
<param name="ignoreInterfaces">false</param>
<param name="root">data</param>
<result type="json"/>
</action>
</package>
和行动:
public class InitAction extends ActionSupport{
private List<Student> data; // set and get
public String init() {
System.out.println("Data" + data );// data is null ( Why ??? )
}
}
和班级学生:
public class Student {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}