//我正在開發一個使用struts2框架的web應用程序。我是新來的JSON,並試圖通過AJAX發送JSON數據,以行動class.But在動作類越來越空值//在struts2動作類中獲取json數據null
第一步: //實現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();
}
}
我們haved使用Struts2的核心 - 2.3.16.jar,Struts2的JSON的 - 插件 - 2.3.16.jar – user3804142
警報或做'JSON.stringify(iprodReportsViewReportSqlQueryDetails)一個的console.log' –
檢查您的JSON格式?把它放在雙引號(鍵,值)中。 – hari