SpringMVC接收和返回参数的最主要的一种方式 -- json

本文介绍如何通过jQuery的Ajax请求与SpringMVC框架结合实现异步查询用户列表的功能。具体包括前端页面数据的组织、Ajax请求的发送、SpringMVC的配置及后端数据处理等步骤。

json 是在实际开发中用的比较多的一种接收参数和返回值的一种方式。

需求:
异步的去查询用户列表。

jquery 的 ajax 请求的代码如下:

<script type="text/javascript"> 
	var userMgr = {
		queryUserList : function() {
			var userName = $('#userName').val();
			$.ajax({
				type : "post",
				url : "<%=path%>/sysmgr/user/getUserList",
				data : "userName=" + userName, // 发送的数据
				dataType : "json", // text,html,xml 返回的是一个json格式的对象
				success : function(data) {
					alert(data);
					var htmlTable = "";
					// alert(JSON.stringify(data));
					if (data.length != 0) {
						for (var i = 0; i < data.length; i++) {
							htmlTable = htmlTable + "<tr>";
							htmlTable = htmlTable + "<td>";
							htmlTable = htmlTable + data[i].userName;
							htmlTable = htmlTable + "</td>";
							htmlTable = htmlTable + "<td>";
							htmlTable = htmlTable + data[i].loginName;
							htmlTable = htmlTable + "</td>";
							htmlTable = htmlTable + "<td>";
							htmlTable = htmlTable + data[i].age;
							htmlTable = htmlTable + "</td>";
							htmlTable = htmlTable + "<td>";
							htmlTable = htmlTable + data[i].sex;
							htmlTable = htmlTable + "</td>";
							var editButton = "<a href='javascript:userMgr.gotoUserEdit("+data[i].userId+")'>修改</a>";
							var delButton = "<a href='javascript:userMgr.delUser("+data[i].userId+")'>删除</a>";
							htmlTable = htmlTable + "<td>";
							htmlTable = htmlTable + editButton;
							htmlTable = htmlTable + "</td>";
							htmlTable = htmlTable + "<td>";
							htmlTable = htmlTable + delButton;
							htmlTable = htmlTable + "</td>";
							htmlTable = htmlTable + "</tr>";
						}
					} else {
						htmlTable = "没有查询到记录";
					}
					$('#userListTable').find('tbody').html(htmlTable);
				}
			});
		}
	}
</script>

由于需要用到 jquery,所以需要引用 js 文件,所以还要对静态资源放行,在 SpringMVC 的配置文件中加上这句(location 为资源文件的位置,mapping 为该资源文件的请求名称):

<mvc:resources location="/jsAndCss/" mapping="/jsAndCss/**"></mvc:resources>

SpringMVC 是这样接收 ajax 请求的数据的:

package com.qjl.ssm.sysmanage.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qjl.ssm.sysmanage.dto.TestDTO;
import com.qjl.ssm.sysmanage.dto.UserDTO;
import com.qjl.ssm.sysmanage.entity.User;
import com.qjl.ssm.sysmanage.service.IUserService;

@Controller
@RequestMapping("/sysmgr/user")
public class UserController {
	
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/getUserList")
	public @ResponseBody List<User> getUserList(String userName) {
		System.out.println(userName);
		List<User> userList = userService.getUserList(null);
		return userList;
	}
}

由于发送过来的只是一个普通的 key - value 的字符串,所以不要加 @RequestBody,加 @RequestBody 表示传输过来的是一个 json 格式的字符串,前端控制器会调用 MappingJackson2HttpMessageConverter 转换器来把传输过来的 json 格式的字符串自动转换成 java 对象,所以,就需要配置 MappingJackson2HttpMessageConverter 转换器,但是由于我们使用的是 mvc 的注解驱动,注解驱动里面已经自动帮我们配置好了转换器,但是由于转换器依赖 jackson 的解析包,因此,我们还需要加入 jackson 的解析包:

  • jackson-annotations-2.5.4.jar
  • jackson-core-2.5.4.jar
  • jackson-databind-2.5.4.jar

才能使 SpringMVC 的 json 转换器 MappingJackson2HttpMessageConverter 起效。

同理,@Responsbody 注解的原理也是一样的,前端控制器在返回的时候就会调用转换器进行转换。

如果前台传送的是 json 格式的字符串,那么需要在 $.ajax({…}) 中添加 contentType : "application/json;charset=UTF-8"

还有需要注意一点:ajax 请求发送过来的数据是字符串,获取到的是一个 json 格式的 js 对象

流程图:
这里写图片描述

数据类型转换过程:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值