Hello Friends hope you all are fine.today i am going to demonstrate you example of subgrid using spring mvc framework.the table structure will look like given below :
DROP TABLE IF EXISTS `studentdb`.`student`;
CREATE TABLE `studentdb`.`student` (
`id` varchar(20) NOT NULL,
`name` varchar(50) NOT NULL,
`college` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `studentdb`.`location`;
CREATE TABLE `studentdb`.`location` (
`id` varchar(20) NOT NULL,
`name` varchar(40) NOT NULL,
`code` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Spring Controller will look given below :
package org.subgrid.controller;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.subgrid.dao.SubgridDaoImpl;
import org.subgrid.handler.JsonMapper;
import org.subgrid.service.SubgridService;
@Controller
public class SubgridController {
//for subgrid demo
@RequestMapping(value = "/getstudentDetails", method = RequestMethod.GET)
public @ResponseBody void getstudentDetails( HttpServletRequest req,HttpServletResponse res,String para) throws IOException
{
//try{
int page = Integer.valueOf(req.getParameter("page"));
int limit =Integer.valueOf(req.getParameter("rows"));
String sidx = req.getParameter("sidx");
String sord =req.getParameter("sord");
WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(req.getSession().getServletContext());
SubgridDaoImpl dao=(SubgridDaoImpl) context.getBean("SubgridDaoImpl");
SubgridService service =(SubgridService) context.getBean("SubgridService");
Map<String, Object>map=service.getstudentDetails(dao, page, limit, sidx, sord, req, para);
JsonMapper mapper=(JsonMapper) context.getBean("JsonMapper");
mapper.WritecInJson(res, map);
}
//for subgrid demo
@RequestMapping(value = "/getLocationDetails", method = RequestMethod.GET)
public @ResponseBody void getLocationDetails(HttpServletRequest req,HttpServletResponse res,String para) throws IOException
{
//try{
String id = req.getParameter("id");
WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(req.getSession().getServletContext());
SubgridDaoImpl dao=(SubgridDaoImpl) context.getBean("SubgridDaoImpl");
SubgridService service =(SubgridService) context.getBean("SubgridService");
Map<String, Object>map=service.getLocationDetails(dao, req, para,id);
JsonMapper mapper=(JsonMapper) context.getBean("JsonMapper");
mapper.WritecInJson(res, map);
}
}
DAO layer will look like given below:
package org.subgrid.dao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.subgrid.entity.LocationDetail;
import org.subgrid.entity.Student;
@Repository
public class SubgridDaoImpl {
private HibernateTemplate template;
public SubgridDaoImpl(HibernateTemplate template) {
this.template = template;
}
public Map<String, Object> getstudentDetails(int page, int limit, String sidx,
String sord, HttpServletRequest req, String para)
{
List<Student> acclist=template.find("from Student");
Map<String, Object> map=new HashMap<String, Object>();
map.put("page", page);
map.put("list", acclist);
return map;
}
public Map<String, Object> getLocationDetails(HttpServletRequest req, String para,
String id) {
List<LocationDetail> acclist=template.find("from LocationDetail where id='"+id+"'");
Map<String, Object> map=new HashMap<String, Object>();
map.put("list", acclist);
return map;
}
}
Service Layer:
package org.subgrid.service;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.subgrid.dao.SubgridDaoImpl;
import org.subgrid.entity.LocationDetail;
import org.subgrid.entity.Student;
@Service
public class SubgridService {
//for subgrid by nisarg pathak
public Map<String, Object> getstudentDetails(SubgridDaoImpl dao, int page,
int limit, String sidx, String sord, HttpServletRequest req,
String para) {
Map<String, Object> map = dao.getstudentDetails(page, limit, sidx, sord,
req, para);
List<Student> list = (List<Student>) map
.get("list");
Map<String, Object> AccountingPeriodMap = new LinkedHashMap<String, Object>();
List<Map<String, Object>> AccountingPeriodList = new ArrayList<Map<String, Object>>();
Map<String, Object> AccountingPeriodSubMap = null;
List<String> cell = null;
int i = 0;
for (Student supp : list) {
i++;
AccountingPeriodSubMap = new LinkedHashMap<String, Object>();
AccountingPeriodSubMap.put("id", i);
cell = new ArrayList<String>();
cell.add(supp.getId()+ "");
cell.add(supp.getName()+ "");
cell.add(supp.getCollege()+ "");
AccountingPeriodSubMap.put("cell", cell);
AccountingPeriodList.add(AccountingPeriodSubMap);
}
AccountingPeriodMap.put("rows", AccountingPeriodList);
AccountingPeriodMap.put("page", map.get("page"));
AccountingPeriodMap.put("total", map.get("total"));
AccountingPeriodMap.put("records", map.get("records"));
return AccountingPeriodMap;
}
//for subgrid demo
public Map<String, Object> getLocationDetails(SubgridDaoImpl dao,
HttpServletRequest req, String para, String id) {
Map<String, Object> map = dao.getLocationDetails(req, para,id);
List<LocationDetail> list = (List<LocationDetail>) map
.get("list");
Map<String, Object> AccountingPeriodMap = new LinkedHashMap<String, Object>();
List<Map<String, Object>> AccountingPeriodList = new ArrayList<Map<String, Object>>();
Map<String, Object> AccountingPeriodSubMap = null;
List<String> cell = null;
int i = 0;
for (LocationDetail supp : list) {
i++;
AccountingPeriodSubMap = new LinkedHashMap<String, Object>();
AccountingPeriodSubMap.put("id", i);
cell = new ArrayList<String>();
cell.add(supp.getId() + "");
cell.add(supp.getName()+ "");
cell.add(supp.getAddress()+ "");
AccountingPeriodSubMap.put("cell", cell);
AccountingPeriodList.add(AccountingPeriodSubMap);
}
AccountingPeriodMap.put("rows", AccountingPeriodList);
AccountingPeriodMap.put("page", map.get("page"));
AccountingPeriodMap.put("total", map.get("total"));
AccountingPeriodMap.put("records", map.get("records"));
return AccountingPeriodMap;
}
}
Json Handler Class:
package org.subgrid.handler;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;
public class JsonMapper {
public void WritecInJson(HttpServletResponse res,Object object)
{
MappingJacksonHttpMessageConverter jsonConverter = new MappingJacksonHttpMessageConverter();
MediaType jsonMimeType = MediaType.APPLICATION_JSON;
if(jsonConverter.canWrite(object.getClass(), jsonMimeType)){
try{
jsonConverter.write(object , jsonMimeType, new ServletServerHttpResponse(res));
}catch(IOException e){
e.printStackTrace();
}catch(HttpMessageNotWritableException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}else{
System.out.println("reached in else");
}
}
}
Entity Classes:
package org.subgrid.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student {
@Id @Column @GeneratedValue
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
@Column
private String name;
@Column
private String college;
public Student() {
// TODO Auto-generated constructor stub
}
}
package org.subgrid.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="location")
public class LocationDetail {
@Id @Column @GeneratedValue
private String id;
@Column
private String name;
@Column
private String code;
@Column
private String address;
public LocationDetail() {
// TODO Auto-generated constructor stub
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Db Configuration Property File:
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/studentdb
jdbc.username = root
jdbc.password = admin!@#
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Optic Solution</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationcontext-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>applicationcontext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>applicationcontext</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
applicationcontext-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="org.subgrid.controller"></context:component-scan>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>classpath:resources/dbConfig.properties</value> </property>
</bean>
<!-- Hibernate Configuration -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>org.subgrid.entity.LocationDetail</value>
<value>org.subgrid.entity.Student</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dbcp.initialSize">8</prop>
<prop key="hibernate.dbcp.maxActive">20</prop>
<prop key="hibernate.dbcp.maxIdle">20</prop>
<prop key="hibernate.dbcp.minIdle">0</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<constructor-arg ref="mySessionFactory" />
</bean>
<bean id="SubgridDaoImpl" class="org.subgrid.dao.SubgridDaoImpl">
<constructor-arg ref="hibernateTemplate" />
</bean>
<bean id="SubgridService" class="org.subgrid.service.SubgridService">
</bean>
<bean id="JsonMapper" class="org.subgrid.handler.JsonMapper"></bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="cacheSeconds" value="0" />
</bean>
</beans>
index.jsp
<link rel="shortcut icon" href="images/icon.png">
<link type="text/css" rel="stylesheet" href="css/layout-default-latest.css" />
<link type="text/css" rel="stylesheet" href="css/bootstrap.min.css" id="default">
<link type="text/css" rel="stylesheet" href="css/font-awesome.min.css">
<link type="text/css" rel="stylesheet" href="css/jquery-ui.css">
<link type="text/css" rel="stylesheet" href="css/style.css" />
<link type="text/css" rel="stylesheet" href="css/ui.jqgrid.css"/>
<link rel="stylesheet" type="text/css" href="css/smart-forms.css">
<link rel="stylesheet" type="text/css" href="css/tabs.css">
<script type="text/javascript" src="script/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="script/bootstrap.js"></script>
<script type="text/javascript" src="script/jquery-ui.min.js"></script>
<script type="text/javascript" src="script/jquery.layout-latest.js"></script>
<script type="text/javascript" src="script/local/grid.locale-en.js" ></script>
<script type="text/javascript" src="script/jquery.jqGrid.min.js"></script>
<script type="text/javascript" src="script/jquery-ui-messageBox.js"></script>
<script type="text/javascript" src="script/jquery.blockUI.js"></script>
<script type="text/javascript" src="script/js/tabs.js"></script>
<script type="text/javascript" src="script/js/javascript.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#list14").jqGrid({
url:'getstudentDetails.html',
datatype: "json",
colNames:['id',' Name','College'],
colModel:[ {name:'id',index:'id'},
{name:'name',index:'name'},
{name:'college',index:'college'}
],
rowNum:10,
rowList:[10,20,30],
pager: '#pager14', sortname: 'id', viewrecords: true,
sortorder: "desc", multiselect: false, subGrid : true,
subGridUrl: 'getLocationDetails.html?q=3',
subGridModel: [{ name : ['id','Name','Address'], width : [55,200,80],
params:['id']} ] ,
caption: "Subgrid with JSON Data" });
jQuery("#list14").jqGrid('navGrid','#pager14',{edit:false,add:false,del:false});
});
</script>
<table id="list14"></table> <div id="pager14"></div>
it will look like given below :
Thanks for Reading this article .. For Any Query Ping me on pathak.nisarg@yahoo.com
Haappy Learning
DROP TABLE IF EXISTS `studentdb`.`student`;
CREATE TABLE `studentdb`.`student` (
`id` varchar(20) NOT NULL,
`name` varchar(50) NOT NULL,
`college` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `studentdb`.`location`;
CREATE TABLE `studentdb`.`location` (
`id` varchar(20) NOT NULL,
`name` varchar(40) NOT NULL,
`code` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Spring Controller will look given below :
package org.subgrid.controller;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.subgrid.dao.SubgridDaoImpl;
import org.subgrid.handler.JsonMapper;
import org.subgrid.service.SubgridService;
@Controller
public class SubgridController {
//for subgrid demo
@RequestMapping(value = "/getstudentDetails", method = RequestMethod.GET)
public @ResponseBody void getstudentDetails( HttpServletRequest req,HttpServletResponse res,String para) throws IOException
{
//try{
int page = Integer.valueOf(req.getParameter("page"));
int limit =Integer.valueOf(req.getParameter("rows"));
String sidx = req.getParameter("sidx");
String sord =req.getParameter("sord");
WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(req.getSession().getServletContext());
SubgridDaoImpl dao=(SubgridDaoImpl) context.getBean("SubgridDaoImpl");
SubgridService service =(SubgridService) context.getBean("SubgridService");
Map<String, Object>map=service.getstudentDetails(dao, page, limit, sidx, sord, req, para);
JsonMapper mapper=(JsonMapper) context.getBean("JsonMapper");
mapper.WritecInJson(res, map);
}
//for subgrid demo
@RequestMapping(value = "/getLocationDetails", method = RequestMethod.GET)
public @ResponseBody void getLocationDetails(HttpServletRequest req,HttpServletResponse res,String para) throws IOException
{
//try{
String id = req.getParameter("id");
WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(req.getSession().getServletContext());
SubgridDaoImpl dao=(SubgridDaoImpl) context.getBean("SubgridDaoImpl");
SubgridService service =(SubgridService) context.getBean("SubgridService");
Map<String, Object>map=service.getLocationDetails(dao, req, para,id);
JsonMapper mapper=(JsonMapper) context.getBean("JsonMapper");
mapper.WritecInJson(res, map);
}
}
DAO layer will look like given below:
package org.subgrid.dao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.subgrid.entity.LocationDetail;
import org.subgrid.entity.Student;
@Repository
public class SubgridDaoImpl {
private HibernateTemplate template;
public SubgridDaoImpl(HibernateTemplate template) {
this.template = template;
}
public Map<String, Object> getstudentDetails(int page, int limit, String sidx,
String sord, HttpServletRequest req, String para)
{
List<Student> acclist=template.find("from Student");
Map<String, Object> map=new HashMap<String, Object>();
map.put("page", page);
map.put("list", acclist);
return map;
}
public Map<String, Object> getLocationDetails(HttpServletRequest req, String para,
String id) {
List<LocationDetail> acclist=template.find("from LocationDetail where id='"+id+"'");
Map<String, Object> map=new HashMap<String, Object>();
map.put("list", acclist);
return map;
}
}
Service Layer:
package org.subgrid.service;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.subgrid.dao.SubgridDaoImpl;
import org.subgrid.entity.LocationDetail;
import org.subgrid.entity.Student;
@Service
public class SubgridService {
//for subgrid by nisarg pathak
public Map<String, Object> getstudentDetails(SubgridDaoImpl dao, int page,
int limit, String sidx, String sord, HttpServletRequest req,
String para) {
Map<String, Object> map = dao.getstudentDetails(page, limit, sidx, sord,
req, para);
List<Student> list = (List<Student>) map
.get("list");
Map<String, Object> AccountingPeriodMap = new LinkedHashMap<String, Object>();
List<Map<String, Object>> AccountingPeriodList = new ArrayList<Map<String, Object>>();
Map<String, Object> AccountingPeriodSubMap = null;
List<String> cell = null;
int i = 0;
for (Student supp : list) {
i++;
AccountingPeriodSubMap = new LinkedHashMap<String, Object>();
AccountingPeriodSubMap.put("id", i);
cell = new ArrayList<String>();
cell.add(supp.getId()+ "");
cell.add(supp.getName()+ "");
cell.add(supp.getCollege()+ "");
AccountingPeriodSubMap.put("cell", cell);
AccountingPeriodList.add(AccountingPeriodSubMap);
}
AccountingPeriodMap.put("rows", AccountingPeriodList);
AccountingPeriodMap.put("page", map.get("page"));
AccountingPeriodMap.put("total", map.get("total"));
AccountingPeriodMap.put("records", map.get("records"));
return AccountingPeriodMap;
}
//for subgrid demo
public Map<String, Object> getLocationDetails(SubgridDaoImpl dao,
HttpServletRequest req, String para, String id) {
Map<String, Object> map = dao.getLocationDetails(req, para,id);
List<LocationDetail> list = (List<LocationDetail>) map
.get("list");
Map<String, Object> AccountingPeriodMap = new LinkedHashMap<String, Object>();
List<Map<String, Object>> AccountingPeriodList = new ArrayList<Map<String, Object>>();
Map<String, Object> AccountingPeriodSubMap = null;
List<String> cell = null;
int i = 0;
for (LocationDetail supp : list) {
i++;
AccountingPeriodSubMap = new LinkedHashMap<String, Object>();
AccountingPeriodSubMap.put("id", i);
cell = new ArrayList<String>();
cell.add(supp.getId() + "");
cell.add(supp.getName()+ "");
cell.add(supp.getAddress()+ "");
AccountingPeriodSubMap.put("cell", cell);
AccountingPeriodList.add(AccountingPeriodSubMap);
}
AccountingPeriodMap.put("rows", AccountingPeriodList);
AccountingPeriodMap.put("page", map.get("page"));
AccountingPeriodMap.put("total", map.get("total"));
AccountingPeriodMap.put("records", map.get("records"));
return AccountingPeriodMap;
}
}
Json Handler Class:
package org.subgrid.handler;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;
public class JsonMapper {
public void WritecInJson(HttpServletResponse res,Object object)
{
MappingJacksonHttpMessageConverter jsonConverter = new MappingJacksonHttpMessageConverter();
MediaType jsonMimeType = MediaType.APPLICATION_JSON;
if(jsonConverter.canWrite(object.getClass(), jsonMimeType)){
try{
jsonConverter.write(object , jsonMimeType, new ServletServerHttpResponse(res));
}catch(IOException e){
e.printStackTrace();
}catch(HttpMessageNotWritableException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}else{
System.out.println("reached in else");
}
}
}
Entity Classes:
package org.subgrid.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student {
@Id @Column @GeneratedValue
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
@Column
private String name;
@Column
private String college;
public Student() {
// TODO Auto-generated constructor stub
}
}
package org.subgrid.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="location")
public class LocationDetail {
@Id @Column @GeneratedValue
private String id;
@Column
private String name;
@Column
private String code;
@Column
private String address;
public LocationDetail() {
// TODO Auto-generated constructor stub
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Db Configuration Property File:
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/studentdb
jdbc.username = root
jdbc.password = admin!@#
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Optic Solution</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationcontext-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>applicationcontext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>applicationcontext</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
applicationcontext-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="org.subgrid.controller"></context:component-scan>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>classpath:resources/dbConfig.properties</value> </property>
</bean>
<!-- Hibernate Configuration -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>org.subgrid.entity.LocationDetail</value>
<value>org.subgrid.entity.Student</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dbcp.initialSize">8</prop>
<prop key="hibernate.dbcp.maxActive">20</prop>
<prop key="hibernate.dbcp.maxIdle">20</prop>
<prop key="hibernate.dbcp.minIdle">0</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<constructor-arg ref="mySessionFactory" />
</bean>
<bean id="SubgridDaoImpl" class="org.subgrid.dao.SubgridDaoImpl">
<constructor-arg ref="hibernateTemplate" />
</bean>
<bean id="SubgridService" class="org.subgrid.service.SubgridService">
</bean>
<bean id="JsonMapper" class="org.subgrid.handler.JsonMapper"></bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="cacheSeconds" value="0" />
</bean>
</beans>
index.jsp
<link rel="shortcut icon" href="images/icon.png">
<link type="text/css" rel="stylesheet" href="css/layout-default-latest.css" />
<link type="text/css" rel="stylesheet" href="css/bootstrap.min.css" id="default">
<link type="text/css" rel="stylesheet" href="css/font-awesome.min.css">
<link type="text/css" rel="stylesheet" href="css/jquery-ui.css">
<link type="text/css" rel="stylesheet" href="css/style.css" />
<link type="text/css" rel="stylesheet" href="css/ui.jqgrid.css"/>
<link rel="stylesheet" type="text/css" href="css/smart-forms.css">
<link rel="stylesheet" type="text/css" href="css/tabs.css">
<script type="text/javascript" src="script/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="script/bootstrap.js"></script>
<script type="text/javascript" src="script/jquery-ui.min.js"></script>
<script type="text/javascript" src="script/jquery.layout-latest.js"></script>
<script type="text/javascript" src="script/local/grid.locale-en.js" ></script>
<script type="text/javascript" src="script/jquery.jqGrid.min.js"></script>
<script type="text/javascript" src="script/jquery-ui-messageBox.js"></script>
<script type="text/javascript" src="script/jquery.blockUI.js"></script>
<script type="text/javascript" src="script/js/tabs.js"></script>
<script type="text/javascript" src="script/js/javascript.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#list14").jqGrid({
url:'getstudentDetails.html',
datatype: "json",
colNames:['id',' Name','College'],
colModel:[ {name:'id',index:'id'},
{name:'name',index:'name'},
{name:'college',index:'college'}
],
rowNum:10,
rowList:[10,20,30],
pager: '#pager14', sortname: 'id', viewrecords: true,
sortorder: "desc", multiselect: false, subGrid : true,
subGridUrl: 'getLocationDetails.html?q=3',
subGridModel: [{ name : ['id','Name','Address'], width : [55,200,80],
params:['id']} ] ,
caption: "Subgrid with JSON Data" });
jQuery("#list14").jqGrid('navGrid','#pager14',{edit:false,add:false,del:false});
});
</script>
<table id="list14"></table> <div id="pager14"></div>
it will look like given below :
Thanks for Reading this article .. For Any Query Ping me on pathak.nisarg@yahoo.com
Haappy Learning
No comments:
Post a Comment