DWR Autosuggetion

Embed Size (px)

Citation preview

  • 8/3/2019 DWR Autosuggetion

    1/10

  • 8/3/2019 DWR Autosuggetion

    2/10

    DWR Autosuggetion

    Page 2

    4. Server side Java objects:...................................................................................................................... 7

    create Java class, which will be called by the DWR javascript: ............................................................... 7

    4.1. SearchAction.java: ....................................................................................................................... 7

    4.2. SearchUtil.java: ............................................................................................................................ 8

    4.3. DatabaseUtil.java ........................................................................................................................ 9

    4.4. CommonConstant.java .............................................................................................................. 10

    5. Index.html: ........................................................................................................................................ 10

    6.Figure: ..................................................................................................................................................... 10

  • 8/3/2019 DWR Autosuggetion

    3/10

    DWR Autosuggetion

    Page 3

    1.IntroductionThe browser side JavaScript objects are implemented by the DWR JavaScript code. This code

    communicates with the servlet at the backend using asynchronous XMLHttpRequest support of

    the browser.

    Operations performed by DWR during remoting include:

    DWR runtime processes a dwr.xml file to determine the objects that needs to be remoted DWR uses Java reflection to find out about the available fields and methods of the

    server-side object

    DWR generates JavaScript code that creates equivalent JavaScript objects within thebrowser

    All methods in the JavaScript object are invoked remotely and handled by the DWRservlet

    Methods that return values are handled by converter within DWR.DWR consists of two main parts:

    A Java Servlet running on the server that processes requests and sends responses back tothe browser.

    JavaScript running in the browser that sends requests and can dynamically update thewebpage.

    which consists of:

    1. A servlet (uk.ltd.getahead.dwr.DWRServlet)2. A set of JavaScript libraries (util.js and engine.js)

    2.JavaScript files that must be included in your application for DWR:2.1. JCustomer.js:

    complete path is //dwr/interface/.js.

    in my application the path is /fas.war/dwr/interface/JCustomer.js and one more javascript file/fas.war/dwr/interface/searchDwr.js

    searchDwr.js

    var nameRegEx = /^([a-zA-Z\-\/&_]){1,50}$/;

    functionpopulateSearchFied(obj){

  • 8/3/2019 DWR Autosuggetion

    4/10

    DWR Autosuggetion

    Page 4

    var textfieldValue = obj.value;

    if(nameRegEx.test(textfieldValue) ==false){removeList();

    return false;}

    JCustomer.getSearchResultAction(textfieldValue,searchCallback);

    removeList();var v=document.getElementById("customerName").value;

    }

    function searchCallback(msg) {if (msg.length > 0) {document.getElementById("suggestions").style.display = "block";var sList = document.getElementById("suggestionsList");

    var ul = document.createElement('ul');

    for (var i = 0; i < msg.length; i++){var li = document.createElement('li');li.innerHTML = msg[i];li.onclick = bindFunction(msg[i]);ul.appendChild(li);}

    sList.appendChild(ul);}

    }

    functionbindFunction(txt) {return function () {fillTextField(txt);};

    }

    function fillTextField(txt) {document.getElementById("customerName").value = txt;document.getElementById("suggestions").style.display = "none";

    removeList();

    }

    function removeList() {var sList = document.getElementById("suggestionsList");var children = sList.childNodes;for (var i = 0; i < children.length; i++) {

    sList.removeChild(children[i]);}

    }

    JCustomer.jsfunction JCustomer () {}

  • 8/3/2019 DWR Autosuggetion

    5/10

  • 8/3/2019 DWR Autosuggetion

    6/10

    DWR Autosuggetion

    Page 6

    Setting debug parameter to true allows you to troubleshoot DWR readily to determine if things

    are working alright.

    3.4. A dwr.xml file,placed also into the WEB-INF directory:Which contains information for DWR. This file specifies the Java classes that should be remoted to

    JavaScript. This file contains:

    As you can see in above code, i have added the creator attribute which will tell the dwr engine to create

    the javascript object named JCustomer, using which we can call the server side methods.

    The above configuration tells DWR that:

    1. com.fas.dwr.action.SearchAction should be remoted to JCustomer JavaScript object2. JavaScript code can create instances remotely which return values and arguments oftype JCustomer will be converted

    What exactly do the JCustomer objects looks like? Next we explore the server side Java

    objects.

  • 8/3/2019 DWR Autosuggetion

    7/10

    DWR Autosuggetion

    Page 7

    4.Server side Java objects:create Java class, which will be called by the DWR javascript:

    4.1. SearchAction.java:Helper method and fields of the com.fas.dwr.action. SearchAction class

    getSearchResultAction(StringfieldValue)

    A method to obtain all of the customer names from the database

    which contains character like searchKey. This method returns

    an string array of Customer objects. On the server-side, this is

    an array of Customer Java objects. DWR will convert them toan array of JCustomer JavaScript objects on the browser side.

    SearchAction.java

    package com.fas.dwr.action;

    import com.fas.dwr.utils.SearchUtil;

    publicclass SearchAction{

    public String[] getSearchResultAction(String fieldValue){

    String dataList[] = null;SearchUtil searchUtil = null;try{

    searchUtil = new SearchUtil();dataList = searchUtil.fetchResult(fieldValue.trim());

    }catch(NullPointerException nullExp){}

    return dataList;}

    }

  • 8/3/2019 DWR Autosuggetion

    8/10

    DWR Autosuggetion

    Page 8

    4.2. SearchUtil.java:Helper method and fields of the com.fas.dwr.utils. SearchUtil class

    fetchResult (StringsearchKey)

    This method takes a string argument searchKey and returnsstring array

    OST_STOCK.CUSTOMER_NAME OST_STOCK is a table name and CUSTOMER_NAME is acoloumn name in database.

    package com.fas.dwr.utils;

    import java.util.List;

    import com.broadvision.data.client.Content;import com.broadvision.data.common.TableData;import com.broadvision.data.sql.SQLCondition;import com.broadvision.data.sql.SQLExpression;import com.broadvision.data.sql.SQLOperator;import com.fas.common.utils.CommonConstants;

    publicclass SearchUtil{

    public String[] fetchResult(String searchKey){

    List returnParamter = null;

    DatabaseUtil dataTranscation = null;SQLCondition sqlCondition = null;SQLExpression sqlExpression = null;String []htmlBuilder = null;

    try{

    sqlExpression = new SQLExpression("OST_STOCK.CUSTOMER_NAME ");

    sqlCondition = new SQLCondition(sqlExpression,SQLOperator.OP_LIKE,newSQLExpression(("'%"+searchKey+"%'")));dataTranscation = new DatabaseUtil();returnParamter =dataTranscation.getContentList(CommonConstants.BV_OST_STOCK_CONTENT_TYPE_ID,

    sqlCondition);if(null != returnParamter){

    htmlBuilder = new String[returnParamter.size()];Content content = null;for(int i=0;i

  • 8/3/2019 DWR Autosuggetion

    9/10

    DWR Autosuggetion

    Page 9

    }

    }catch(Exception ee){

    }

    return htmlBuilder;}

    4.3. DatabaseUtil.javaHelper method and fields of the com.fas.dwr.utils. DatabaseUtil class:

    DatabaseUtil A constructor which contains a static block .getContentList Returns a List based on query condition.

    this util class obtain customer names from the database which contains keyword like searchKey.

    package com.fas.dwr.utils;

    import java.util.ArrayList;

    import java.util.List;

    import com.broadvision.data.client.DataManager;

    import com.fas.common.utils.CommonConstants;

    public class DatabaseUtil{

    private static DataManager dataManager=null;

    static{

    try{

    dataManager=new DataManager();

    }catch(Exception e){

    System.out.println(">>>>> Data Base initialization failed while init data manager");

    }

    }public List getContentList(int contentId,SQLCondition condition){

    List contentList = null;try{

    contentList = new ArrayList();contentList = dataManager.getContentsByCondition(contentId,

    CommonConstants.FAS_SERVICE_ID, condition, true);

    }catch(Exception ee){

    }return contentList;

    }

    }

  • 8/3/2019 DWR Autosuggetion

    10/10

    DWR Autosuggetion

    Page 10

    4.4. CommonConstant.javapackage com.fas.common.utils;

    publicclass CommonConstants {

    publicstaticfinalintBV_OST_STOCK_CONTENT_TYPE_ID=1002;

    publicstaticfinalintFAS_SERVICE_ID=92;

    }

    5.Index.html:create index.html using below code:

    Search

    Name:

    6.Figure:

    DWR makes Java objects appear as remote JavaScript objects: