[英]Struts2 JSON to Java Action Object (can't find the error)
I have tried multiple versions and read several related answers, but I still cant figure out why Struts is not populating my Action property user
. 我已经尝试了多个版本并阅读了一些相关的答案,但是我仍然无法弄清楚为什么Struts没有填充Action属性user
。
This is my ajax call 这是我的ajax电话
function save_new_user() {
var user =
{ username: $('#new_user_username').val(),
email: $('#new_user_email').val(),
password: $('#new_user_password').val()
};
user_json = JSON.stringify(user);
console.log(user_json);
var data = {'user': user_json};
$.ajax({
type : 'GET',
url : 'SaveNewUser',
data: data,
dataType : "json",
contentType: 'application/json',
success : function(data, textStatus, jqXHR) {
if(data) {
}
},
});
}
which, by the way, print this in the console 顺便说一句,在控制台中打印
{"username":"dd","email":"ff","password":"gg"}
My Action class (with annotations), (I am not modifying the json-default
interceptor from struts2-json-plugin-2.3.24.1) is 我的Action类(带有注释)(我没有从struts2-json-plugin-2.3.24.1修改json-default
拦截器)是
package coproject.cpweb.actions;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
import coproject.cpweb.utils.db.entities.UserDum;
import coproject.cpweb.utils.db.services.DbServicesImp;
@Action("SaveNewUser")
@ParentPackage("json-default")
@Results({
@Result(name="success", type="json"),
@Result(name="input", location="/views/error.jsp")
})
public class SaveNewUser extends ActionSupport{
private static final long serialVersionUID = 1L;
/* Services */
DbServicesImp dbServices;
public DbServicesImp getDbServices() {
return dbServices;
}
public void setDbServices(DbServicesImp dbServices) {
this.dbServices = dbServices;
}
/* Input Json */
private UserDum user = new UserDum();
public UserDum getUser() {
return user;
}
public void setUser(UserDum user) {
this.user = user;
}
/* Execute */
public String execute() throws Exception {
// dbServices.saveUser(user);
System.out.println(user.getUsername());
return "SUCCESS";
}
}
And the UserDum
entity is 并且UserDum
实体是
package coproject.cpweb.utils.db.entities;
public class UserDum {
private String username;
private String email;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
However, when the request arrives, the json
interceptor of struts-json-plugin gets an exception trying to set "user". 但是,当请求到达时,struts-json-plugin的json
拦截器会尝试设置“用户”时遇到异常。
feb 04, 2016 12:20:14 PM com.opensymphony.xwork2.interceptor.ParametersInterceptor error
SEVERE: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'user' on 'class coproject.cpweb.actions.SaveNewUser: Error setting expression 'user' with val
ue ['{"username":"dd","email":"ff","password":"gg"}', ]
feb 04, 2016 12:20:14 PM com.opensymphony.xwork2.util.LocalizedTextUtil warn
WARNING: Missing key [invalid.fieldvalue.user] in bundles [[org/apache/struts2/struts-messages, com/opensymphony/xwork2/xwork-mess
ages]]!
Any clues on what might be the error? 关于可能是什么错误的任何线索?
Extending json-default
package doesn't set json
interceptor to the defaultStack
of interceptors, it just defines that there is such interceptor. 扩展json-default
包不会将json
拦截器设置为defaultStack
的拦截器,它只是定义存在此类拦截器。
Use interceptorRefs
in your @Action
annotation to set json
interceptor and the defaultStack
to the action. 在@Action
批注中使用interceptorRefs
可以将json
拦截器和defaultStack
设置为该动作。
@Action(value="SaveNewUser",
interceptorRefs={ @InterceptorRef("json"),
@InterceptorRef("defaultStack") })
Or @InterceptorRefs
at the class level to apply interceptors to all actions defined in that class. 或在类级别使用@InterceptorRefs
将拦截器应用于该类中定义的所有操作。
@InterceptorRefs({
@InterceptorRef("json"),
@InterceptorRef("defaultStack")
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.