Aniket Aniket - 4 days ago 6
jQuery Question

Set Action Class attribute using ajax jquery Struts-1

I am facing issue in setting action class String through ajax call. Here is my code

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 call is as follows:

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


When action calls I am getting testString as null. I have tried my best to solve it but can't find what is missing. Any is is greatly appreciated.

Answer

Define a your ActionForm class

 public class Person extends ActionForm {

        private String testString;

        public String getTestString() {
            return testString;
        }

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



    }

Your Struts config will look like this

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

Change action class method like that

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

Check with these changes hopefully it will work

Comments