0% found this document useful (0 votes)
82 views11 pages

Pathvariable AJAX

This document provides code examples for performing CRUD operations in a Spring MVC application using AJAX and Hibernate. It includes Java controller and service classes to handle requests and interact with the database via Hibernate. It also includes JSP and configuration files to define the web application and integrate Spring and Hibernate.

Uploaded by

Prabhakar Prabhu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
82 views11 pages

Pathvariable AJAX

This document provides code examples for performing CRUD operations in a Spring MVC application using AJAX and Hibernate. It includes Java controller and service classes to handle requests and interact with the database via Hibernate. It also includes JSP and configuration files to define the web application and integrate Spring and Hibernate.

Uploaded by

Prabhakar Prabhu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

AJAX: Example Program:

<%@ page language="java" contentType="text/html;


charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<script src="http://code.jquery.com/jquery-
1.11.2.min.js"></script>
<script type="text/javascript">
function makeCall(){
var input1=$("#abcd").val();
$.ajax({
url:'ajaxCall',
data:'input='+input1,
success:function(message){
$("#myDivArea").html(message);
}

});

</script>

<title>Insert title here</title>


</head>
<body>
<div id="myDivArea"></div>

<input type="button" value="Show Out" onclick="return


makeCall();">
<input type="text" name="abcd" id="abcd">
</body>
</html>
package com.app;

import org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {

@RequestMapping("/welcome")
public ModelAndView showPage(){

return new ModelAndView("welcome");

@RequestMapping("/ajaxCall")
public @ResponseBody String
provideResponse(@RequestParam("input")String input){

return input+" "+Math.random()*10000;


}

}
<?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"
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">

<servlet>
<servlet-name>sample</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servle
t-class>
</servlet>

<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context
"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/be
ans
http://www.springframework.org/schema/beans/spring-beans-
3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-
3.0.xsd">

<context:annotation-config/>
<context:component-scan base-package="com.app"/>

<bean
class="org.springframework.web.servlet.view.InternalResourceVi
ewResolver">
<property name="prefix" value="/WEB-INF/jsps/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>
Delete Employee
Using Path
variable:
package com.app;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {

@Autowired
@Qualifier("empService")
private IEmpService empService;

@RequestMapping(value="/register")
public ModelAndView insertRecord(){
return new ModelAndView("Register");
}
@RequestMapping(value="/home/
{empId}",method=RequestMethod.GET)
public ModelAndView
sayHello(@PathVariable("empId")String empid){
System.out.println("Entered vales is"+empid);
return new ModelAndView("Page1");
}

@RequestMapping(value="/insert")
public ModelAndView
insertRecord(@ModelAttribute("employee")Employee emp){
empService.insertEmployee(emp);
List<Employee>
empList=empService.getAllEmployee();

return new
ModelAndView("Register","empList",empList);
}

@RequestMapping("/delete/{empId}")
public ModelAndView
deleteRecord(@PathVariable("empId")Integer empId){
empService.deleteEmployee(empId);
List<Employee>
empList=empService.getAllEmployee();

return new
ModelAndView("Register","empList",empList);
}

package com.app;

import java.util.List;
public interface IEmpService {

public Integer insertEmployee(Employee emp);


public List<Employee> getAllEmployee();
public void deleteEmployee(Integer empId);
}

----------]
package com.app;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("empService")
public class EmpServiceImpl implements IEmpService {

@Autowired
@Qualifier("empDao")
private IEmpDao empDao;

@Override
public Integer insertEmployee(Employee emp) {
return empDao.insertEmployee(emp);
}

@Override
public List<Employee> getAllEmployee() {
return empDao.getAllEmployee();
}

@Override
public void deleteEmployee(Integer empId) {
empDao.deleteEmployee(empId);
}

}
package com.app;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("empService")
public class EmpServiceImpl implements IEmpService {

@Autowired
@Qualifier("empDao")
private IEmpDao empDao;

@Override
public Integer insertEmployee(Employee emp) {
return empDao.insertEmployee(emp);
}

@Override
public List<Employee> getAllEmployee() {
return empDao.getAllEmployee();
}

@Override
public void deleteEmployee(Integer empId) {
empDao.deleteEmployee(empId);

package com.app;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="emp")
public class Employee {

@Id
@Column(name="eid")
private Integer empId;
@Column(name="ename")
private String empName;

public Integer getEmpId() {


return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}

}
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="insert">
<pre>
User Id :<input type="text" name="empId" id="empId">
User Name: <input type="text" name="empName"
id="empName">
<input type="submit" value="Register">
</pre>
</form>

<c:forEach items="${empList}" var="emp">


<c:out value="${emp.empId}"/>
<c:out value="${emp.empName}"/>
<a href='delete/<c:out value="$
{emp.empId}"/>'/>Delete</a>

<br/>
</c:forEach>

</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context
"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/be
ans
http://www.springframework.org/schema/beans/spring-beans-
3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-
3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.app"/>

<bean
class="org.springframework.web.servlet.view.InternalResourceVi
ewResolver">
<property name="prefix" value="/WEB-INF/jsps/"/>
<property name="suffix" value=".jsp"/>
</bean>

<bean name="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerData
Source">
<property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>

<bean name="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.Annotatio
nSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.app.Employee</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop
key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</pro
p>
<prop
key="hibernate.hbm2ddl.auto">update</prop>
<prop
key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<bean
class="org.springframework.orm.hibernate3.HibernateTemplate"
name="template">
<property name="sessionFactory"
ref="mySessionFactory"/>

</bean>

</beans>
<?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"
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">

<servlet>
<servlet-name>sample</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servle
t-class>
</servlet>

<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>

You might also like