GoogleSearchBox

Custom Search

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

1 comment: