我正在尝试将用户定义的对象发布到struts2的操作,但我的操作对象仍为null。谁能帮我?谢谢你的帮助。
User.java
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
这是我的行动 Login.java
public class Login extends ActionSupport {
private User user;
@Override
public String execute() throws Exception {
System.out.println(user.getUsername() + " - " + user.getPassword());
return "success";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
我在jsp页面中的ajax调用:
$(function() {
var user = {'user.username': 'abc', 'user.password' : '123'};
//var user = {username: 'abc', password : '123'};
$.ajax({
type : 'POST',
url : 'login',
data : JSON.stringify(user),
//data: user,
contentType : 'application/json',
success : function(result) {
alert("hh");
}
});
});
struts.xml中
<struts>
<constant name="struts.devMode" value="true" />
<package name="struts2jquery" extends="struts-default, json-default">
<action name="login" class="com.myapp.action.Login">
<result name="success">result.jsp</result>
</action>
</package>
</struts>
答案 0 :(得分:2)
最后,我解决了它:D。只需删除ajax调用中的行contentType: "application/json"
即可。
现在的ajax调用是:
var u = {'user.username': 'abc', 'user.password' : '123'};
$.ajax({
type : 'POST',
url : 'login',
data: u,
//contentType: "application/json",
success : function(result) {
//alert("hh");
}
});
谢谢大家:D