使用原生JavaScriptAjax以及jQuery的Ajax结合SpringMVC发送和获取json数据

一.原生Ajax

1.在WEB-INF/lib目录中加入spring和json与Jackson的jar文件,json与Jackson的jar文件如下:

使用原生JavaScriptAjax以及jQuery的Ajax结合SpringMVC发送和获取json数据_第1张图片

2.配置web.xml文件:配置前端控制器



  SpringMVC_1
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
  springmvc
  org.springframework.web.servlet.DispatcherServlet
  
  	contextConfigLocation
  	classpath:springmvc.xml
  
  
  1
  
  
  springmvc
  *.action
  

3.配置SpringMVC的xml文件:springmvc.xml



	
	
	
	
	
	
	
	
	
		
		
			/WEB-INF/
		
		
		
			.jsp
		
	



4.编写实体类:Users.java


package com.beans;

public class Users {
	private Integer id;
	private String name;
	public Users() {
		// TODO Auto-generated constructor stub
	}
	public Users(Integer id,String name) {
		this.id = id;
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

}

5.编写前端html和Ajax代码:


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


编号 姓名

6.编写处理类handler:MyHandler.java


package com.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.beans.Users;

import net.sf.json.JSONObject;


@Controller
public class MyHandler {
	
	@RequestMapping("/post")
	public void returnBack2(@RequestBody Users user,HttpServletResponse response) throws IOException{
		JSONObject json = JSONObject.fromObject(user);
		response.getWriter().println(json.toString());
	}
}

二.jQuery的Ajax

1.导入jar包以及web.xml、springmvc.xml的配置同上面原生Ajax。在WebContent下新建一个名为js的文件夹,向其中导入使用jQuery以及json2的需要用到的js文件,比如jquery-1.11.1.min.js和json2.js:


2.编写实体类:Book.java


package com.domain;

import java.io.Serializable;

public class Book implements Serializable{

	private Integer id;
	private String name;
	private String author;
	public Book() {
		// TODO Auto-generated constructor stub
	}
	public Book(Integer id,String name, String author) {
		this.id = id;
		this.name = name;
		this.author = author;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	@Override
	public String toString() {
		return "Book [id=" + id + ", name=" + name + ", author=" + author + "]";
	}
	
}

3.编写html和jQuery代码:


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




测试接收JSON格式的数据






编号:
书名:
作者:

4.编写处理类handler:BookController.java



package com.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.domain.Book;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class BookController {

	private static final Log logger = LogFactory.getLog(BookController.class);
	
	@RequestMapping("/testRequestBody")
	public void setJson(@RequestBody Book book,HttpServletResponse response) throws IOException{
		ObjectMapper mapper = new ObjectMapper();
		logger.info(mapper.writeValueAsString(book));
		book.setAuthor("张三");
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().println(mapper.writeValueAsString(book));
	}
}






你可能感兴趣的:(JavaEE)