我有一个struts 2的应用程序,我想使用ajax调用action类来感知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动作类。请帮助。
答案 0 :(得分:1)
您需要在struts.xml中定义结果类型,如下所示。
<result type="json" />
如果您仍然遇到任何问题,请告诉我