Spring 3.0.5+JSON+Ajax

配置参考: http://xiaoyaozjl.iteye.com/blog/1457182
源码参考: http://xace.iteye.com/blog/731263
这里是另一种方法: http://javaest.iteye.com/blog/187504

特别注意:SpringMVC 3.0.5
去掉SpringMVC的自带包:com.springsource.org.codehaus.jackson.mapper-1.0.0.jar
加入:[ http://www.jarvana.com/jarvana/browse/org/ 到这里去找]
jackson-core-asl-1.6.4.jar
jackson-mapper-asl-1.6.4.jar

<!-- 配置SpringMVC @ResponseBody和@RequestBody注解 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="jsonHttpMessageConverter" />
			</list>
		</property>
	</bean>
	<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
	</bean>






package com.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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;

import com.pojo.Users;

@Controller
public class JSONController {
	@RequestMapping(value = "getUserListFromServer", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUserList() {

		Map<String, Object> modelMap = new HashMap<String, Object>(3);
		modelMap.put("total", "1");
		modelMap.put("data", getUsers());
		modelMap.put("success", "true");
		return modelMap;
	}

	public List<Users> getUsers() {

		List<Users> list = new ArrayList<Users>();
		for (int i = 0; i < 5; i++) {
			Users user = null;
			user = new Users();
			user.setName("Pandy " + i);
			user.setPassword("pandy " + i);
			user.setEmail("[email protected] " + i);
			user.setRemarks("Nothing " + i);
			user.setId(i);
			list.add(user);
		}
		return list;
	}

	@RequestMapping(value = "commitToServer", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> addUser(@RequestBody Users user) {
		System.out.println(user.getName());
		Map<String, String> map = new HashMap<String, String>(1);
		map.put("success", "true");
		return map;
	}

}






getFromServer = function() {
    alert("getFromServer");
    $.ajax({
        type : "GET",
        url : "getUserListFromServer.do",
        data : "name=John&location=Boston",
        contentType : 'application/json', //这里可以不加上,但是要是提交的是json对象字符串的话,必须加上.
        dataType : 'json',//这里可以不加上,但是要是提交的是json对象字符串的话,必须加上.
        success : function(obj) {
            alert("Data Saved: " + JSON.stringify(obj));
        },
        error: function(obj, s1, s2){
            alert(JSON.stringify(obj));
        },
        complete : function(obj, s1){
            alert("complete:"+s1);
        }
    });
}
commitToServer = function() {
    alert("commitToServer");
    var obj = {};
    obj.name="Pandy";
    obj.email="[email protected]";
    var param = JSON.stringify(obj);
    alert(param);
    $.ajax({
        type : "POST",
        url : "commitToServer.do",
        data : param,
        contentType : 'application/json', //这里必须要加上.
        dataType : 'json',  
        success : function(obj) {
            alert(JSON.stringify(obj));
        },
        error: function(obj, s1, s2){
            alert(JSON.stringify(obj));
        },
        complete : function(obj, s1){
            alert("complete:"+s1);
        }
    });
}


你可能感兴趣的:(Spring 3.0.5+JSON+Ajax)