1. 项目中需要引入如下两个jar包:
jackson-core-asl-1.7.2jar
jackson-mapper-asl-1.7.2jar2. SpringMVC配置文件中修改:
<!-- 支持spring3.0新的mvc注解 -->
<mvc:annotation-driven />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="cacheSeconds" value="0" />
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean>
注意:如果引入的jackson-mapper-as包版本较新,尝试使用这种配置:
<!-- jackson-mapper-asl-1.9.13.jar -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="cacheSeconds" value="0" />
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</list>
</property>
</bean>
3. 客户端代码a.jsp如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script>
function createAjaxObj(){
var req;
if(window.XMLHttpRequest){
req = new XMLHttpRequest();
}else{
req = new ActiveXObject("Msxml2.XMLHTTP"); //ie
}
return req;
}
function sendAjaxReq(){
var req = createAjaxObj();
req.open("get","myajax.do?method=test1&uname=张三");
req.setRequestHeader("accept","application/json");
req.onreadystatechange = function(){
eval("var result="+req.responseText);
document.getElementById("div1").innerHTML=result[0].uname;
}
req.send(null);
}
</script>
</head>
<body>
<a href="javascript:void(0);" οnclick="sendAjaxReq();">测试</a>
<div id="div1"></div>
</body>
</html>
4. 服务器端代码如下:
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
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.sxt.po.User;
@Controller
@RequestMapping("myajax.do")
public class MyAjaxController {
/*处理Ajax请求,加@ResponseBody即可*/
@RequestMapping(params="method=test1",method=RequestMethod.GET)
public @ResponseBody List<User> test1(String uname) throws Exception{
String uname2 = new String(uname.getBytes("iso8859-1"),"gbk");
System.out.println(uname2);
System.out.println("MyAjaxController.test1()");
List<User> list = new ArrayList<User>();
list.add(new User("user1","123"));
list.add(new User("user2","456"));
return list;
}
}