views:

50

answers:

2

i want to send a jsp page which consist of some divs and table as part of ajax response from spring frame work, is there any way to send jsp as an response of ajax call

A: 

Yes, there's no magic to this though. In your java ajax handler just return a forward or redirect to the jsp page you want. The response will then be available as the responseText in your ajax callback.

You can use jsp to generate just the elements you need as a sort of incomplete HTML fragment then return this from your server side handler. Then in your javascript callback you can insert the fragment into your existing HTML likeThis

element.innerHTML = resp.responseText 
//element is the parent you want to insert to 
//resp is the parameter supplied to your callback
Ollie Edwards
+2  A: 

Sending JSP by AJAX makes no sense, it is basically the HTML generated by a JSP that is sent over to the browser through AJAX, as rightly pointed out by thelost.

You do not need any server-side coding for this. All you need is to write some javascript on client side to receive your HTML asynchronously. For this, I would recommend using some javascript framework like jQuery, otherwise it would make your life hell.

Assuming that the page you want to access by AJAX has the link http://domain:port/mypage.htm. First you need to include jQuery in your base JSP (JSP in which former page has to load by AJAX):

<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.js"></script>

Then you need to call jQuery's ajax function:

$(document).ready(function(){
    $.ajax({
                        type:"GET",
                        url: "http://domain:port/mypage.htm",
                        success: function(data){
                            // Now you have your HTML in "data", do whatever you want with it here in this function         
                            alert(data);
                        }
                    });
});

Hope it helps!

craftsman
also consider using $.load() instead if you are trying to replace a portion of code with AJAX, works very nicely in conjunction with JSP, you can even fill your Model and use that model in the AJAX JSP. I actually have a folder within my JSP folder named AJAX, to show that those JSP's should not be rendered by themselves, but rather as part of other pages
walnutmon