SpringMVC下配置JSON

1. 项目中需要引入如下两个jar包:

jackson-core-asl-1.7.2jar

jackson-mapper-asl-1.7.2jar

2. 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;
	}
	
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值