Thursday, June 14, 2012

calling ajax in jsr 286

I have created a sample portlet to show the suggestion by calling ajax:-









The sample jsp for calling the ajax as above is:


<%@page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1" session="false"%>
<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
<script type="text/javascript">
  function createXMLHttpRequestObject() {
    var xmlHttp;
    try {
      xmlHttp = new XMLHttpRequest();
    } catch (e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
      } catch (e) {
      }
    }
    if (!xmlHttp)
      alert("Error creating the XMLHttpRequest object.");
    else
      return xmlHttp;
  }
  var xmlHttp = createXMLHttpRequestObject();
 
 
  function makeResourceCall(str){
 
 
  if (str.length==0)
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
 
    if (xmlHttp){
    var newvalue ="Harish"+str;
 
      try{
      var url ="<portlet:resourceURL></portlet:resourceURL>?txtValue="+str;
   
        xmlHttp.open("GET", url, true);
        xmlHttp.onreadystatechange = handleRequestStateChange;
       
        xmlHttp.send(null);
      }catch (e){
        alert("Can't connect to server:\n" + e.toString());
      }
    }
  }

  function handleRequestStateChange(){
    if (xmlHttp.readyState == 4){
      if (xmlHttp.status == 200){
     
      if(xmlHttp.responseText == "")
      {
      document.getElementById("txtHint").style.display="none";
      }
      else
      {
       document.getElementById("txtHint").style.display="block";
       document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
       }
      }
    }
  }
</script>

Enter Name<input type="text" name="txtValue" onkeyup="JavaScript:makeResourceCall(this.value)" >


 <!-- <p>Suggestions: <span id="txtHint"></span></p>  -->

<TEXTAREA name="annotationTxt" rows="10" cols="30" style="overflow:hidden;" id="txtHint" maxlength="235"></TEXTAREA>

<script>
function onload()
{
 document.getElementById("txtHint").style.display="none";
}
onload();
</script>


Onkeyup I am calling makeResourceCall and from makeResourceCall function calling <portlet:resourceURL>
From this the control will go the portlet class and will call the serverResource method:

 public void serveResource(ResourceRequest request, ResourceResponse response)
     throws PortletException, IOException {
     System.out.println("Entering ValidationCacheSamplePortlet.serveResource()");
     response.setContentType("text/html");
     System.out.println("String input value"+request.getParameter("txtValue"));
     inserRec rec = new inserRec();
     List list = rec.getData(request.getParameter("txtValue"));
    
    for (Iterator iterator = list.iterator(); iterator.hasNext();) 
    {
    HibernateprjPojo pojo = (HibernateprjPojo) iterator.next();
response.getWriter().println(pojo.getName());
In the serve resource method I am making a hibernate call and fetching the related values for txtValue.
From the getwritter the result will be shown in the drop down box.


No comments:

Post a Comment