//我正在使用struts2框架开发Web应用程序。我是json的新手,正尝试通过ajax将json数据发送到操作类,但是在操作类中获取null值//

步骤1:
//已实现json3.min.js //

var iprodReportsViewReportSqlQueryDetails={
    rtemplateSqlquery : sqlQuery,
    jdbcDriverUrl : jdbcDriverUrlG,
    jdbcDriverClassname : jdbcDriverClassnameG,
    databaseInstanceName : databaseInstanceNameG,
    databaseServerName : databaseServerUsernameG,
    databaseServerPassword : databaseServerPasswordG,
};

$.ajax({
    type : "POST",
    url : 'validateBuiltSqlQueryAction',
    cache : false,
    async : false,
    dataType : 'json',
    contentType:"application/json;charset=utf-8",
    data :  JSON.stringify(iprodReportsViewReportSqlQueryDetails),
    success : function(data) {
            // we have the response
            alert("successs");

    },
    error : function(e) {
            alert("error occured");
    }
});


/*********This is the action class where the json data is to be received***********/
/* We have used gson for deserialization. */
public class IprodReportsSqlQueryMasterAction extends ActionSupport {

    protected String rtemplateSqlquery;
    protected String jdbcDriverClassname;
    protected String jdbcDriverUrl;
    protected String databaseInstanceName;
    protected String databaseServerName;
    protected String databaseServerPassword;
    protected String data;
    public String validateSqlQuery() {
            Gson gs=new Gson();
            System.out.println("Data received:"+ getData());

IprodReportsViewReportSqlQueryDetails iprodReportsViewReportSqlQueryDetailsObject=gs.fromJson(data, IprodReportsViewReportSqlQueryDetails.class);
             System.out.println(iprodReportsViewReportSqlQueryDetailsObject.getRtemplateSqlquery());
             System.out.println(iprodReportsViewReportSqlQueryDetailsObject.getJdbcDriverClassname());
             System.out.println(iprodReportsViewReportSqlQueryDetailsObject.getJdbcDriverUrl());
             System.out.println(iprodReportsViewReportSqlQueryDetailsObject.getDatabaseInstanceName());
             System.out.println(iprodReportsViewReportSqlQueryDetailsObject.getDatabaseServerName());
             System.out.println(iprodReportsViewReportSqlQueryDetailsObject.getDatabaseServerPassword());

        return SUCCESS;
        }


    public String getRtemplateSqlquery()
    {
        return rtemplateSqlquery;
    }

        public void setRtemplateSqlquery(String rtemplateSqlquery)
    {
        this.rtemplateSqlquery = rtemplateSqlquery;
    }


    public String getJdbcDriverClassname()
    {
        return jdbcDriverClassname;
    }


    public void setJdbcDriverClassname(String jdbcDriverClassname)
    {
        this.jdbcDriverClassname = jdbcDriverClassname;
    }


    public String getJdbcDriverUrl()
    {
        return jdbcDriverUrl;
    }


    public void setJdbcDriverUrl(String jdbcDriverUrl)
    {
        this.jdbcDriverUrl = jdbcDriverUrl;
    }


    public String getDatabaseInstanceName()
    {
        return databaseInstanceName;
    }


    public void setDatabaseInstanceName(String databaseInstanceName)
    {
        this.databaseInstanceName = databaseInstanceName;
    }


    public String getDatabaseServerName()
    {
        return databaseServerName;
    }


    public void setDatabaseServerName(String databaseServerName)
    {
        this.databaseServerName = databaseServerName;
    }


    public String getDatabaseServerPassword()
    {
        return databaseServerPassword;
    }


    public void setDatabaseServerPassword(String databaseServerPassword)
    {
        this.databaseServerPassword = databaseServerPassword;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

}

/*****struts.xml***************//
 <package name="json-default-package" extends="json-default">
        <result-types>
            <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
        </result-types>
        <interceptors>
            <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>
        </interceptors>

</package>

               <package extends="json-default-package" name="validateSqlQuery" namespace="/">
            <action class="com.id4.iprod.reports.IprodReportsSqlQueryMasterAction" method="validateSqlQuery" name="validateBuiltSqlQueryAction">

                     <interceptor-ref name="json">
                      <param name="contentType">application/JSON</param>
                      </interceptor-ref>
                      <result name="success" type="json"></result>
                  </action>


    </package>

   /***********************************************/
/****bean object class  IprodReportsViewReportSqlQueryDetails***** //

public class IprodReportsViewReportSqlQueryDetails implements Serializable
{

    protected String rtemplateSqlquery;

        protected String jdbcDriverClassname;

        protected String jdbcDriverUrl;

        protected String databaseInstanceName;

        protected String databaseServerName;


    protected String databaseServerPassword;


    public IprodReportsViewReportSqlQueryDetails()
    {
    }


    public String getRtemplateSqlquery()
    {
        return rtemplateSqlquery;
    }


    public void setRtemplateSqlquery(String rtemplateSqlquery)
    {
        this.rtemplateSqlquery = rtemplateSqlquery;
    }

        public String getJdbcDriverClassname()
    {
        return jdbcDriverClassname;
    }

    public void setJdbcDriverClassname(String jdbcDriverClassname)
    {
        this.jdbcDriverClassname = jdbcDriverClassname;
    }

        public String getJdbcDriverUrl()
    {
        return jdbcDriverUrl;
    }


    public void setJdbcDriverUrl(String jdbcDriverUrl)
    {
        this.jdbcDriverUrl = jdbcDriverUrl;
    }

    public String getDatabaseInstanceName()
    {
        return databaseInstanceName;
    }

        public void setDatabaseInstanceName(String databaseInstanceName)
    {
        this.databaseInstanceName = databaseInstanceName;
    }


    public String getDatabaseServerName()
    {
        return databaseServerName;
    }


    public void setDatabaseServerName(String databaseServerName)
    {
        this.databaseServerName = databaseServerName;
    }


    public String getDatabaseServerPassword()
    {
        return databaseServerPassword;
    }


    public void setDatabaseServerPassword(String databaseServerPassword)
    {
        this.databaseServerPassword = databaseServerPassword;
    }

        public boolean equals(Object _other)
    {
        if (_other == null) {
            return false;
        }

        if (_other == this) {
            return true;
        }

        if (!(_other instanceof IprodReportsViewReportSqlQueryDetails)) {
            return false;
        }

        final IprodReportsViewReportSqlQueryDetails _cast = (IprodReportsViewReportSqlQueryDetails) _other;
        if (rtemplateSqlquery == null ? _cast.rtemplateSqlquery != rtemplateSqlquery : !rtemplateSqlquery.equals( _cast.rtemplateSqlquery )) {
            return false;
        }

        if (jdbcDriverClassname == null ? _cast.jdbcDriverClassname != jdbcDriverClassname : !jdbcDriverClassname.equals( _cast.jdbcDriverClassname )) {
            return false;
        }

        if (jdbcDriverUrl == null ? _cast.jdbcDriverUrl != jdbcDriverUrl : !jdbcDriverUrl.equals( _cast.jdbcDriverUrl )) {
            return false;
        }

        if (databaseInstanceName == null ? _cast.databaseInstanceName != databaseInstanceName : !databaseInstanceName.equals( _cast.databaseInstanceName )) {
            return false;
        }

        if (databaseServerName == null ? _cast.databaseServerName != databaseServerName : !databaseServerName.equals( _cast.databaseServerName )) {
            return false;
        }

        if (databaseServerPassword == null ? _cast.databaseServerPassword != databaseServerPassword : !databaseServerPassword.equals( _cast.databaseServerPassword )) {
            return false;
        }

        return true;
    }

        public int hashCode()
    {
        int _hashCode = 0;
        if (rtemplateSqlquery != null) {
            _hashCode = 29 * _hashCode + rtemplateSqlquery.hashCode();
        }

        if (jdbcDriverClassname != null) {
            _hashCode = 29 * _hashCode + jdbcDriverClassname.hashCode();
        }

        if (jdbcDriverUrl != null) {
            _hashCode = 29 * _hashCode + jdbcDriverUrl.hashCode();
        }

        if (databaseInstanceName != null) {
            _hashCode = 29 * _hashCode + databaseInstanceName.hashCode();
        }

        if (databaseServerName != null) {
            _hashCode = 29 * _hashCode + databaseServerName.hashCode();
        }

        if (databaseServerPassword != null) {
            _hashCode = 29 * _hashCode + databaseServerPassword.hashCode();
        }

        return _hashCode;
    }


    public String toString()
    {
        StringBuffer ret = new StringBuffer();
        ret.append( "com.id4.iprod.reports.dto.IprodReportsViewReportSqlQueryDetails: " );
        ret.append( "rtemplateSqlquery=" + rtemplateSqlquery );
        ret.append( ", jdbcDriverClassname=" + jdbcDriverClassname );
        ret.append( ", jdbcDriverUrl=" + jdbcDriverUrl );
        ret.append( ", databaseInstanceName=" + databaseInstanceName );
        ret.append( ", databaseServerName=" + databaseServerName );
        ret.append( ", databaseServerPassword=" + databaseServerPassword );
        return ret.toString();
    }

}

最佳答案

步骤1:在您的jsp页面复制中粘贴完整代码

$.ajax({
type : "POST",
url : 'validateBuiltSqlQueryAction',
cache : false,
async : false,
data :  "data="+JSON.stringify(iprodReportsViewReportSqlQueryDetails),
dataType : 'json',
success : function(data) {
        // we have the response
        alert("successs");

},
failure: function(result){
    console.log("FAILED");
    console.log(result);
}
});


步骤2:在struts.xml

 <action class="com.id4.iprod.reports.IprodReportsSqlQueryMasterAction" method="validateSqlQuery" name="validateBuiltSqlQueryAction">
       <result name="success" type="json"></result>
 </action>


输出:
在控制台中


收到的数据:
{“ rtemplateSqlquery”:“ sqlQuery”,“ jdbcDriverUrlG”:“ jdbcDriverUrlG”,“ jdbcDriverClassname”:“ jdbcDriverClassnameG”,“ databaseInstanceName”:“ databaseInstanceNameG”,“ databaseServerName”:“ databaseServerUsernameG”,“ databaseServerPassword”:“”
sqlQuery
jdbcDriverClassnameG
jdbcDriverUrlG
databaseInstanceNameG
databaseServerUsernameG
databaseServerPasswordG


在jsp上


您将收到成功警报。

10-04 20:16