2014-07-04 53 views
0

//我正在開發一個使用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(); 
    } 

} 
+0

我們haved使用Struts2的核心 - 2.3.16.jar,Struts2的JSON的 - 插件 - 2.3.16.jar – user3804142

+0

警報或做'JSON.stringify(iprodReportsViewReportSqlQueryDetails)一個的console.log' –

+0

檢查您的JSON格式?把它放在雙引號(鍵,值)中。 – hari

回答

0

第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」, 「jdbcDriverUrl」: 「jdbcDriverUrlG」, 「jdbcDriverClassname」: 「jdbcDriverClassnameG」,」 databaseInstanceName「:」databaseInstanceNameG「,」databaseServerName「:」databaseServerUsernameG「,」databaseServerPassword「:」databaseServerPasswordG「}
sqlQuery
jdbcDriverClassnameG
jdb cDriverUrlG
databaseInstanceNameG
databaseServerUsernameG
databaseServerPasswordG

JSP的

你會得到成功的警報。

+0

你的解決方案工作。謝謝Parth .. – user3804142

+0

如果你是新的堆棧溢出,我會建議你學習如何接受答案。看到這個鏈接.- [點擊這裏](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) –