2016-11-29 77 views
0

我在通過ajax調用設置動作類String時遇到問題。這裏是我的代碼使用ajax設置Action類屬性jquery Struts-1

DashBoardGraphAction.java

public class DashBoardGraphAction extends DispatchAction 
{ 
    private String testString; 


    public ActionForward getTransactionsByStatus(ActionMapping mapping, ActionForm form, 
      HttpServletRequest request, HttpServletResponse response) 
        throws Exception 
    { 
     System.out.println(fromDate); 
     return null; 
    } 

    public String getTestString() { 
     return testString; 
    } 
    public void setTestString(String testString) { 
     this.testString = testString; 
    } 

Ajax調用如下:

<script> 
      var testString = "testName"; 
      $.ajax({ 

       url: '${pageContext.request.contextPath}/gettransactionData.do?parameter=getTransactionsByStatus', 

       type: 'POST', 
       data: 'testString='+testString, 
       dataType: 'json', 
       success: function(data) 
       {  
        str = data; 
       }, 
       async:false, 
       error: function(XMLHttpRequest, textStatus, errorThrown){ 
       alert('Error ' + textStatus); 
       alert(errorThrown); 
       alert(XMLHttpRequest.responseText); 
       } 
      }); 
</script> 

struts-config.xml中

<action path="/gettransactionData" parameter="parameter" 
    type="com.pp.dashboard.DashBoardGraphAction"> 
    <forward name = "getTransactionsByStatus" path = "/jsp/pp/dashboardReportStatus.jsp" /> 
    <forward name = "getTransactionsByProcessor" path = "/jsp/pp/dashboardReportProcessorWise.jsp" /> 
</action> 

當操作調用我得到的TestString爲空。我盡力解決它,但找不到缺失的東西。任何是非常感謝。

+1

你在哪裏定義了你的ActionForm我猜你是在提交testString作爲Post主體?但我無法看到你的動作形式定義,其中動作將填充此值 – Bilal

回答

1

定義你的ActionForm類

public class Person extends ActionForm { 

     private String testString; 

     public String getTestString() { 
      return testString; 
     } 

     public void setTestString(String testString) { 
      this.testString = testString; 
     } 



    } 

你的Struts配置看起來像這樣

public ActionForward getTransactionsByStatus(ActionMapping mapping, ActionForm form, 
      HttpServletRequest request, HttpServletResponse response) 
        throws Exception 
    { 
     Person p = (Person) form; 
     System.out.println("Testing************************"+p.getTestString()); 
     return null; 
    } 

檢查這些變化這個

<struts-config> 
     <form-beans> 

      <form-bean name="formClass" type="com.forms.Person"/> 

     </form-beans> 

     <action-mappings> 



      <action name="formClass" path="/gettransactionData" parameter="parameter" 
    type="com.pp.dashboard.DashBoardGraphAction"> 
    <forward name = "getTransactionsByStatus" path = "/jsp/pp/dashboardReportStatus.jsp" /> 
    <forward name = "getTransactionsByProcessor" path = "/jsp/pp/dashboardReportProcessorWise.jsp" /> 
</action> 


     </action-mappings> 

    </struts-config> 

更改動作類的方法希望它會工作

+0

現在可以工作。謝謝比爾。 – Aniket