0

我正在使用 struts2 。当我将 JsonObject 从 js 发送到 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;
}


}
4

0 回答 0