Spring MVC with AJAX implementation using Jackson API and Jquery
The following jar files needed for this ,
commons-logging-1.1.1.jar
jackson-core-asl-1.4.0150249.jar
jackson-mapper-asl-1.4.3.jar
jstl-1.2.jar
spring-asm-3.0.3.RELEASE.jar
spring-beans-3.0.3.RELEASE.jar
spring-context-3.0.3.RELEASE.jar
spring-context-support-3.0.3.RELEASE.jar
spring-core-3.0.3.RELEASE.jar
spring-expression-3.0.3.RELEASE.jar
spring-web-3.0.3.RELEASE.jar
spring-webmvc-3.0.3.RELEASE.jar
Ajax implementation is based on
jackson-core-asl-1.4.0150249.jar and jackson-mapper-asl-1.4.3.jar
1) Added this jars in your classpath.
2) Add the following configuration in your context file,
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter"/>
</list>
</property>
</bean>
package com.jijo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class EmployeeController {
@RequestMapping(value = "/SaveEmploye.html", method = RequestMethod.POST)
public @ResponseBody String saveEmployee(@RequestBody Employe employe) {
if(employe != null){
System.out.println("*****DATA IS NOT NULL*****: "+employe);
for(Address address :employe.getAddresses()){
System.out.println("Employee Name:"+employe.getFirstName()+" \nHouseName:"+address.getHouseName());
}
}
else{
System.out.println("employe is null");
}
return "done";
}
}
package com.jijo;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Employe implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String employeeId;
private String firstName;
private String lastName;
private String department;
private List<Address> addresses = new ArrayList<Address>();
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
}
5) Addess.java
package com.jijo;
import java.io.Serializable;
public class Address implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String houseName;
private String place;
private String district;
private String state;
private String country;
private String postalCode;
public String getHouseName() {
return houseName;
}
public void setHouseName(String houseName) {
this.houseName = houseName;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
}
6) Javascript function
function saveEmploye(){
var addresses = [];
var employee = {"employeeId":"10","firstName":"John","lastName":"Smith","department":"IT"};
var address =
{"houseName":"HouseNo-10",
"place":"PLACE1",
"district":"district1","state":"state1"};
addresses.push(address);
employee.addresses=addresses;
$.ajax({
type: "post",
url: "SaveEmploye.html", //your url
contentType: "application/json",
data: JSON.stringify(employee),
success: function(result) {
alert('Employee saved successfully');
},
error: function(){
alert('Error occured');
}
});
}
The following jar files needed for this ,
commons-logging-1.1.1.jar
jackson-core-asl-1.4.0150249.jar
jackson-mapper-asl-1.4.3.jar
jstl-1.2.jar
spring-asm-3.0.3.RELEASE.jar
spring-beans-3.0.3.RELEASE.jar
spring-context-3.0.3.RELEASE.jar
spring-context-support-3.0.3.RELEASE.jar
spring-core-3.0.3.RELEASE.jar
spring-expression-3.0.3.RELEASE.jar
spring-web-3.0.3.RELEASE.jar
spring-webmvc-3.0.3.RELEASE.jar
Ajax implementation is based on
jackson-core-asl-1.4.0150249.jar and jackson-mapper-asl-1.4.3.jar
1) Added this jars in your classpath.
2) Add the following configuration in your context file,
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter"/>
</list>
</property>
</bean>
3) Controller Class(EmployeeController.java)
package com.jijo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class EmployeeController {
@RequestMapping(value = "/SaveEmploye.html", method = RequestMethod.POST)
public @ResponseBody String saveEmployee(@RequestBody Employe employe) {
if(employe != null){
System.out.println("*****DATA IS NOT NULL*****: "+employe);
for(Address address :employe.getAddresses()){
System.out.println("Employee Name:"+employe.getFirstName()+" \nHouseName:"+address.getHouseName());
}
}
else{
System.out.println("employe is null");
}
return "done";
}
}
4) Employe.java
package com.jijo;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Employe implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String employeeId;
private String firstName;
private String lastName;
private String department;
private List<Address> addresses = new ArrayList<Address>();
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
}
5) Addess.java
package com.jijo;
import java.io.Serializable;
public class Address implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String houseName;
private String place;
private String district;
private String state;
private String country;
private String postalCode;
public String getHouseName() {
return houseName;
}
public void setHouseName(String houseName) {
this.houseName = houseName;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
}
6) Javascript function
function saveEmploye(){
var addresses = [];
var employee = {"employeeId":"10","firstName":"John","lastName":"Smith","department":"IT"};
var address =
{"houseName":"HouseNo-10",
"place":"PLACE1",
"district":"district1","state":"state1"};
addresses.push(address);
employee.addresses=addresses;
$.ajax({
type: "post",
url: "SaveEmploye.html", //your url
contentType: "application/json",
data: JSON.stringify(employee),
success: function(result) {
alert('Employee saved successfully');
},
error: function(){
alert('Error occured');
}
});
}
2 comments:
You don't need to explicitly add jars and update your servlet context as the latest spring versions got the classes required.
Good post:-)
Post a Comment