Making AJAX call using DWR (Direct Web Remoting)

This Article will explain how to make DWR (Direct Web Remoting) AJAX call from JSP

Follow the simple Steps
a         Copy the DWR jar file to the Lib directory under webcontent/WEB-INF (download from here)
b        Copy the following xml snippet to the web.xml

c         Create or Copy the dwr.xml under the WEB-INF directory and content of the file is as follows
    "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"

    <create creator="new" javascript=" AjaxDWRTest">
      <param name="class" value="com.vaka.siva.helper.AjaxDWRTest"/>

d        Write a class with public methods and className  should be like as specified in dwr.xml (If you need the  HTTPServletSession then we use below extra code)

public class AjaxDWRTest {
public String getDisplayedTickets(int pageIndex) {
                                                WebContext ctx = WebContextFactory.get();
                                                HttpServletRequest request = ctx.getHttpServletRequest();
                                                HttpSession session = ctx.getSession();
                                                if (session == null)return null;
//call to some helpers and do the processing
//you can make backend calls now or you can make backend calls first and store it in the session and now retrieve from session

return tktstr
e        In above XML , we have specified the AjaxDWRTest as javascript ,so DWR will create  that for us we just need to include the following in JSP where we need the AJAX call

<script type="text/javascript" src='<%= renderResponse.encodeURL(renderRequest.getContextPath()+ "/dwr/interface/AjaxDWRTest.js") %>'>
<script type="text/javascript" src='<%= renderResponse.encodeURL(renderRequest.getContextPath()+ "/dwr/engine.js") %>'>

f          Following is the way we invoke the callback method in JSP

//Print the table with initial values List (this is not ajax call)
<c:if test="${!empty SessionBean.initialTicketValuesMap}">
<script type="text/javascript">

//following is AJAX call , when somebody click next page then we call below
AjaxDWRTest.getDisplayedTickets(currentPageIndex, <portlet:namespace/>paintDynaSec);

//Once AJAX backend call is over then above statement will automatically calls paintDynaSec javascript function with parameter that is returned from the AJAX
function <portlet:namespace/>paintDynaSec(tktstr) {
                        ticketList.innerHTML =tktstr;

No comments:

Post a Comment