2

我已经尝试了多个版本并阅读了几个相关的答案,但我仍然无法弄清楚为什么 Struts 没有填充我的 Action 属性user

这是我的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) {

            }
        },
    });
}

顺便说一下,在控制台中打印这个

{"username":"dd","email":"ff","password":"gg"}

我的 Action 类(带注释),(我没有json-default从 struts2-json-plugin-2.3.24.1 修改拦截器)是

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";
    }


}

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;
    }
}

但是,当请求到达时,jsonstruts-json-plugin 的拦截器在尝试设置“用户”时遇到异常。

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]]!

关于可能是什么错误的任何线索?

4

2 回答 2

2

扩展json-default包不会将json拦截器设置defaultStack为拦截器,它只是定义有这样的拦截器。

interceptorRefs在您的@Action注释中使用来设置json拦截器和defaultStack操作。

@Action(value="SaveNewUser", 
        interceptorRefs={ @InterceptorRef("json"), 
                          @InterceptorRef("defaultStack") })

或者@InterceptorRefs在类级别将拦截器应用于该类中定义的所有操作。

@InterceptorRefs({
    @InterceptorRef("json"),
    @InterceptorRef("defaultStack")
})
于 2016-02-04T11:45:12.553 回答
0

我也遇到了同样的错误,使用时这是一个愚蠢的错误POSTMANBody type应该设置为JSON,对我来说,选择了默认的TEXT类型选项。

前提是我使用了 JSON 拦截器。

<action name="sample" class="com.wildcard.action.SampleController">
            <interceptor-ref name="json"/>
             <interceptor-ref name="defaultStack"/>

<!-- declarations for mappings from Request JSON Body goes here-->
            <result type="json">
                <param name="root">key_from_json_body</param>
            </result>
        </action>
于 2022-02-07T15:35:04.967 回答