AJAX in Spring MVC:-
..............................................................................................
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script>
function searchdata(param)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if(param!='')
{
document.getElementById("res").innerHTML = xmlhttp.responseText;
}
else
{
document.getElementById("res").innerHTML = '';
}
}
xmlhttp.open("post","ajaxcode.do?q="+param,true);
xmlhttp.send();
}
</script>
</head>
<body>
<input type="text" placeholder="enter character" onkeyup="searchdata(this.value)" />
<div id="res"></div>
</body>
</html>
package controllers;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import dbhelper.Datahelper;
import models.Emp;
@Controller
public class AjaxController {
@RequestMapping("loadajax")
public String searchview()
{
return "loadajax";
}
@RequestMapping("ajaxcode")
public ModelAndView ajaxcode(HttpServletRequest request)
{
Datahelper.connect();
return new ModelAndView("ajaxoutput","key",Datahelper.searchOperation(Emp.class,request.getParameter("q")));
}
}
ajaxoutput.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.util.*,models.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Welcome in AJAX</h1>
<table border='1' id="tabledata">
<tr><th>ID</th><th>Name</th><th>Job</th><th>Username</th><th>Password</th></tr>
<%
List data = (List)request.getAttribute("key");
Iterator it = data.iterator();
while(it.hasNext())
{
Emp obj = (Emp)it.next();
%>
<tr><td><%= obj.getEmpid() %></td><td><%= obj.getEmpname() %></td><td><%= obj.getJob() %></td><td><%= obj.getUsername() %></td><td><%= obj.getPassword() %></td><td><a href="empfind.do?q=<%= obj.getEmpid() %>">Edit</a></td><td><a href="">Delete</a></tr>
<%
}
%>
</table>
</body>
</html>
Create Helper class
package dbhelper;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import models.Emp;
public class Datahelper {
static Configuration cfg;
static SessionFactory sf;
static Session sess;
static Transaction tx;
public static void connect()
{
cfg= new Configuration();
cfg.configure("hibernate.cfg.xml");
sf = cfg.buildSessionFactory();
sess = sf.openSession();
}
public static void insertOperation(Object obj)
{
tx= sess.beginTransaction();
sess.save(obj);
tx.commit();
}
public static void updateOperation(Object obj)
{
tx= sess.beginTransaction();
sess.update(obj);
tx.commit();
}
public static void deleteOperation(Object obj)
{
tx= sess.beginTransaction();
sess.delete(obj);
tx.commit();
}
public static Query selectOperation(String query)
{
Query q = sess.createQuery(query);
return q;
}
public static List searchOperation(Class c,String args)
{
Criteria q = sess.createCriteria(c);
Criterion c1=Restrictions.like("empname",args+"%");
q.add(c1);
return q.list();
}
public static Object findOperation(Class c,Integer id)
{
Object o = sess.get(c,id);
return o;
}
public static void closeConn()
{
sess.close();
}
}

POST Answer of Questions and ASK to Doubt