Custom Search

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

        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) {
        error: function (request, status, error) {
 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) {
  return SUCCESS;

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

Tuesday, May 6, 2014

load a jsp thorugh jquery or load jsp with struts tags through jquery

Basically you can not create a jsp using jquery. Because jquery is valid on client side while JSP is valid on Server side.
So how to load a JSP through jquery ??

You can make an AJAX call to a server to get the html version (compiled jsp into an html as the response) of the jsp response and you use the response.

You can use Jquery ajax (get or post), or load function to do that:

For example using :


       success:function ( data ) //where data is the response in format of html f//rom your jsp/action/servlet
         $("#resultDiv").html( data );
     } );

where assuming you have a div with its id as "resultDiv" where you want to insert the response html contents.

And if you are thinking, what if I my request is being handled by a Servlet class on the server end not directly by a JSP!

Then you have to do, just do whatever business logic you want to process in the Servlet, then before sending the response you just forward the request to the jsp which will process the request for the presentation/UI layer.

request.getRequestDispatcher("/WEB-INF/myxyz.jsp").forward(request, response);

 request.getRequestDispatcher("/jsp/myxyz.jsp").forward(request, response);

If you are using Struts/Struts2, process the business logic in your action but configure the Action mapping for the success (or any, which you want to be processed through the jsp) result, something like this:

<result name="success" type="dispatcher">
   <param name="location">jsp/myxyz.jsp</param>

Now in your jquery's ajax or load method you will get the html version of the jsp, so load that html response into a div or show in a dialog box or manipulate the content as you wish.

Hope this helps!


 load jsp with struts tags through jquery
create jsp with struts tags through jquery
create part of a jsp with struts tags through jquery
load a jsp thorugh jquery

Struts2 action called twice or multiple of times

Struts2 action gets called twice or multiple of times specially when dealing with Json:

I used to get issues while calling Struts2 action using jQuery Ajax and passing JSON to and from Action.

Reason: Struts2 uses a lot interceptors in its default stack to pre and post process the request.
And in particular JSONInterceptor will try to call all the getter methods available  using java Refelection api  inside the Action class (which makes it multiple calls) and tries to serialize the objects returned from the getter methods.

     You need to refactor/rename your getter methods with some other meaningful names like loadXXXXX / showXXXX.


struts2 action multiple methods execute
struts2 action  automatically calling many times
struts2 action class automatically calling many times
why my struts2 action class invoke previous execute method
struts2 action calls previous method
struts2 filter mapping calls many methods