GoogleSearchBox

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

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 :
$("#resultDiv").load("/myjspfromserver.jsp")

OR

$.ajax(
    {
       url:url,
       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);

Or
 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>
</result>



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!





References:
http://stackoverflow.com/questions/15111588/how-do-i-load-jsp-with-jquery-load-function-when-the-jsp-is-under-web-inf-fold
http://stackoverflow.com/questions/3015335/jquery-html-vs-append

Tags:
 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.

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




Reference:
http://stackoverflow.com/questions/6061709/struts2-action-being-called-twice-if-result-type-is-json


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

Tuesday, December 10, 2013

Eclipse compiler error: The method ... of type ... must override a superclass method

I got some error as below in eclipse complaining about a compiler error:
The method ..... of type ..... must override a superclass method.
Where as as you can clearly that I am overriding the method using the @Override annotation
 


Reason:
Check if your eclipse/myeclipse is using a compiler version lesser than java 1.6.

Solution:
Change the compiler to jdk 1.6 or above in your IDE as shown in below picture:
That should do.




References: http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips

Wednesday, August 28, 2013

format java date from string MM/dd/yyyy HH:mm:ss a

String datetimeString = "6/7/2013 2:55:44 PM";  // A String representing your date
java.util.Date result = new Date();
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");

// Below Will Throw Exception in thread "main" java.text.ParseException: Unparseable date:
// SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy"); 

result = formatter.parse(datetimeString); // Throws ParseException
System.out.println("Parse Java Util Date  = "+result);
System.out.println("formatted: " + formatter.format(result));


Output is:
Parse Java Util Date  = Fri Jun 07 14:55:44 GMT+05:30 2013

formatted: 06/07/2013 02:55:44 PM



Refer below posts for more examples:
http://viralpatel.net/blogs/check-string-is-valid-date-java/
http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html

Tuesday, August 20, 2013

How to pass multiple property (option) values as an argument to a method in JQuery

For an jQuery API we have multiples of options to use from for a method.
For example, for the jQuery UI Dialog widget we have a lot of options to choose from like we have maxHeight, minWidth, modal, resizable, draggable to achieve different functionalyties with the widget.

So how to use all or some of these options with the dialog method ?

Here is how we can do it:
We can create a variable containing all the options (name:value) with comma(,) in between multiple values (basically json notation) and then pass the variable to the dialog() method.

  $(function(){
 var dialogOpts = {
  show: true,
  hide: true,
  maxHeight: 400,
  minWidth: 850
 };
 $("#dialog").dialog(dialogOpts);
  });

where "dialog" is the div name inside your html's body as follows :
<div id="dialog" title="Basic dialog">

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

</div>




Tags:
jquery option chain
jquery ui option chaining
jquery method option chaining
How to use multiple options to pass to a method in JQuery
How to use multiple options using options for jQuery method
How to use multiple options to pass to a method in JQuery

What is the dollar Sign ($) means in JQuery Or how to avoid $ collision from jquery with other languages or technologies

$ (dollar) sign in Jquery is just a method name or identifier name.

As javascript allows the $ sign as a special character including some other special characters to be used as a valid variable name (identifier) or a valid method name.

To make it simple, jQuery and some other languages (scripting or coding) has choosen this $ to use their api (methods) easily.

In jQuery, we can replace the $ sign with the word "jQuery" whenever we encounter $ collision issues with some other coding languages like JSTL (JSP, EL) or prototype etc. and it will do the trick.
Infact we can say, $ is a shortcut symbol for the "jQuery" word.

So in jQuery below code :
<script>
  $(function() {
    $( "#dialog" ).dialog();
  });
</script>

is Same as below code:
<script>
  jQuery(function() {
    jQuery( "#dialog" ).dialog();
  });
</script>



Tags:
What is the dollar Sign ($) means in JQuery ?
How to avoid $ collision from jquery with other languages or technologies ?

Friday, July 12, 2013

How to know whether a WebService WSDL file is of SOAP version 1.1 or 1.2 compliant

If your WSDL file has a namespace named "soap12" inside the <wsdl:definitions   tag, as shown below:

<wsdl:definitions xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"



Then the wsdl is SOAP 1.2 version compliant /compatible.


References:
http://schemas.xmlsoap.org/wsdl/soap12/soap12WSDL.htm