One of the person was asking me to post a Spring MVC JSON example in stackoverflow. I am demonstrating only the code on the fly, not in formal way.
public ModelAndView JsonExample(HttpServletRequest request,
HttpServletResponse response) throws Exception {
HashMap map = new HashMap ();
String pattern = request.getParameter("tag");
ArrayList < Map < String , Object > > list = distributorService.getCustomerList();
if (pattern != null && !pattern.equalsIgnoreCase("")) {
ArrayList < Map < String , Object > > list1 = new ArrayList < Map < String , Object > >();
for (int i = 0; i < list.size(); i++) {
if ((((String) list.get(i).get("caption")).toLowerCase()).contains(pattern.toLowerCase()))
list1.add(list.get(i));
}
map.put("customerList", list1);
} else {
map.put("customerList", null);
}
return new ModelAndView(new JSONView(), map);
}
And then create a service interface and implement that interface:
public ArrayList < Map < String , Object > > getCustomerList(){
List list = distributorDao.getCustomerList();
ArrayList < Map < String , Object > > listN = new ArrayList < Map < String , Object > >();
Map < String , Object > info =null;
String type="";
for(int i = 0;i < list.size();i++){
info = new HashMap < String , Object >();
Object[] obj = (Object[]) list.get(i);
info.put("value", obj[0]);
info.put("caption", obj[1]);
listN.add(info);
}
return listN;
}
And later add the DAO interface and implement that:
public List getCustomerList(){
String queryStr = "select customerid,customer_name from customer_tbl";
Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
Query query = session.createSQLQuery(queryStr);
List list = query.list();
return list;
}
--- Hossain Doula