我有一个带有 struts 2 的应用程序,我想使用 ajax 调用操作类来感知 json。我的 ajax 看起来像这样:
var data = {{userName:"userName",password:"password",userId:"userId"},
{userName:"userName",password:"password",userId:"userId"},
{userName:"userName",password:"password",userId:"userId"}
}
var users= JSON.stringify(data);
$.ajax({
data: users,
type: "POST",
url: "http://localhost:8080/Example_Project/Example.action",
contentType: "application/json",
dataType: "json"
})
Action.class
public class ExampleAction extends ActionSupport
{
private List<User> users;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public String addUsers(){
//code to inser user list
return "success";
}
}
User.java
public class User{
private String ;//getters and setters for it
private String password;//getters and setters for it
private Integer userId;//getters and setters for it
}
struts.xml
<struts>
<package name="default" extends="struts-default,json-default" >
<action name="Example" class="com.json.action.ExampleAction" method="addUsers">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
我正在使用 struts2-core 2.3.1.2、struts2-json-plugin 2.3.1.2 和 json-rpc 1.0。如何在操作类中获取用户数据。使用上面的代码,我在操作类中的用户变量中得到了 null 。请帮忙。