GoogleSearchBox

Custom Search
Showing posts with label Ajax. Show all posts
Showing posts with label Ajax. Show all posts

Thursday, May 29, 2014

use jquery ajax to call an action struts2

My JS function to call Action through jquery:


function getData(plNum, plName, code){
   var myJson = {};
  myJson.planNumber = plNum;
  myJson.planName = plName;
  myJson.code = code;
 var jsonString  = JSON.stringify(myJson); /* using json string in the "data" below does not work  */
 console.log(jsonString);

 $.ajax({
        url: "getABCPlans",
        data: {"planNumber" : plNum, "planName" : plName, "code" : code}, /*jsonString,*/    /*dataType: 'json',*/  /*  Not needed, if not returning a json and not sending as a json, needs json interceptor */

        type: 'GET',
        /*contentType: 'application/json;charset=utf-8',*/  /*  Not needed, if not returning a json and not sending as a json, needs json interceptor */
        success: function (res) {
         showAnalystPlans(res);
        },
        error: function (request, status, error) {
            console.log(status);
        }
    });
 return false;
}

Where Action class ("MyPlanAction" class) has the corresponding method as:
public String getPlans() {
  String planNum = getPlanNumber(); /*getter and setter of "planNumber" */
  String planName = getPlanName(); /*getter and setter of "planName" */
  String code = getCode(); /* getter and setter of "code" */
  MyBO bo = new MyBO ();
   try {
    bo.seList(MyService.getPlans(planNum, planName, code));
   } catch (Exception exception) {
    logger.error(exception.getMessage());
   }
  return SUCCESS;
 }


And my struts config has entries:
<action name="getABCPlans" class="com.my.action.MyPlanAction"
  method="getPlans">
    <interceptor-ref name="defaultStack"></interceptor-ref>
 <result name="success" type="dispatcher">
     <param name="location">WEB-INF/pages/plansPage.jsp</param>
 </result>
 </action>