springmvc接收请求参数(springmvc教程二)

版权声明:随意转载。 https://blog.csdn.net/dengjili/article/details/84498732

工程代码

github: https://github.com/dengjili/springmvc

普通接收请求参数

请求地址: http://localhost:8080/springmvc/param/normalParam?name=%张三&note=mmp

	// 如果参数名称与url中保持一致,则无需注解,自动映射
	// 适用于参数个数较少
	@RequestMapping("/normalParam")
	public ModelAndView normalParam(String name, String note) {
		logger.debug("name: {}", name);
		logger.debug("note: {}", note);

		ModelAndView mv = new ModelAndView();
		mv.setViewName("index");
		return mv;
	}

pojo接收请求参数

请求地址: http://localhost:8080/springmvc/param/beanParam?name=abc&note=mmp2

	// pojo中的属性与url中保持一致,则无需注解,自动映射
	// 适用于参数个数较多,封装成bean对象
	@RequestMapping("/beanParam")
	public ModelAndView beanParam(BeanParam beanParam) {
		logger.debug("name: {}", beanParam.getName());
		logger.debug("note: {}", beanParam.getNote());

		ModelAndView mv = new ModelAndView();
		mv.setViewName("index");
		return mv;
	}

BeanParam

public class BeanParam {
	
	private String name;
	private String note;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	
}

@RequestParam接收请求参数

请求地址: http://localhost:8080/springmvc/param/requestParam?pre_name=ab&pre_note=ha

	// url和参数名称不一致,使用@RequestParam进行参数转换
	// boolean required() default true;
	@RequestMapping("/requestParam")
	public ModelAndView requestParam(@RequestParam("pre_name") String name, @RequestParam("pre_note") String note) {
		logger.debug("name: {}", name);
		logger.debug("note: {}", note);

		ModelAndView mv = new ModelAndView();
		mv.setViewName("index");
		return mv;
	}

restful风格 URL接收请求参数

请求地址: http://localhost:8080/springmvc/param/get/1236787/张三

	// restful风格 关键字:@RequestMapping, @PathVariable
	@RequestMapping(value = "/get/{id}/{name}", method = RequestMethod.GET)
	public ModelAndView get(@PathVariable("id") String id, @PathVariable("name") String name) {
		logger.debug("name: {}", id);
		logger.debug("name: {}", name);

		ModelAndView mv = new ModelAndView();
		mv.setViewName("index");
		return mv;
	}

前置页面 jquery.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>复杂参数学习</title>
	<script type="text/javascript" src="${pageContext.request.contextPath }/resource/js/jquery-1.11.3.min.js"></script>
	<script type="text/javascript">
		
		$(function($) {
			$("#json").bind("click", function() {
				var data = {
					name : "mmp",
					param : {
						start : 1,
						limit : 10
					}
				};
				// 发送json数据
				$.ajax({
					type : "POST",
					url : "requestBody",
					data : JSON.stringify(data),
					contentType : 'application/json',
					success : function(data) {
						alert(JSON.stringify(data));
					}
				});
			});

			$("#list").bind("click", function() {
				// []认为是数组
				var data = [ 1, 2, 4 ];
				// 发送json数据
				$.ajax({
					type : "POST",
					url : "listArray",
					data : JSON.stringify(data),
					contentType : 'application/json',
					success : function(data) {
						alert(JSON.stringify(data));
					}
				});
			});

			$("#custlist").bind("click", function() {
				// []认为是数组,自定义类型
				var data = [ {
					start : 1,
					limit : 10
				}, {
					start : 5,
					limit : 20
				}, {
					start : 100,
					limit : 200
				} ];
				// 发送json数据
				$.ajax({
					type : "POST",
					url : "listCust",
					data : JSON.stringify(data),
					contentType : 'application/json',
					success : function(data) {
						alert(JSON.stringify(data));
					}
				});
			});

			$("#commit").bind("click", function() {
				// []认为是数组
				var data = [ 1, 2, 4 ];
				// 发送json数据
				$.ajax({
					type : "GET",
					url : "serialize",
					data : $("#form").serialize(),
					success : function(data) {
						alert(JSON.stringify(data));
					}
				});
			});
		});
	</script>
</head>
<body>
	<h2>json</h2>
	<button type="button" id="json">json</button>
	<hr>
	<h2>列表数据</h2>
	<button type="button" id="list">列表数据</button>
	<hr>
	<h2>自定义类型列表</h2>
	<button type="button" id="custlist">自定义类型列表</button>
	<hr>
	<h2>表单序列化</h2>
	<form id="form" action="serialize">
		<table>
			<tr>
				<td>名称</td>
				<td><input id="name" name="name" value=""/></td>
			</tr>
			<tr>
				<td>备注</td>
				<td><input id="note" name="note" value=""/></td>
			</tr>
			<tr>
				<td></td>
				<td align="right"><input id="commit" type="submit" value="点击"/></td>
			</tr>
		</table>
	</form>
</body>
</html>

json接收请求参数

请求地址: 详见jquery.jsp文件

	// json数据传输
	@RequestMapping(value = "/requestBody", method = RequestMethod.POST)
	public ModelAndView requestBody(@RequestBody ComplexParam complexParam) {
		// 数据域
		ModelAndView mv = new ModelAndView();
		mv.addObject(complexParam);
		mv.setView(new MappingJackson2JsonView());
		return mv;
	}

Array数据接收请求参数

请求地址: 详见jquery.jsp文件

	// list Array数据传输
	@RequestMapping(value = "/listArray", method = RequestMethod.POST)
	public ModelAndView listArray(@RequestBody List<Long> array) {
		// 数据域
		ModelAndView mv = new ModelAndView();
		mv.addObject(array);
		mv.setView(new MappingJackson2JsonView());
		return mv;
	}
	// list Array数据传输(自定义类型)
	@RequestMapping(value = "/listCust", method = RequestMethod.POST)
	public ModelAndView listCust(@RequestBody List<PageParam> array) {
		// 数据域
		ModelAndView mv = new ModelAndView();
		mv.addObject(array);
		mv.setView(new MappingJackson2JsonView());
		return mv;
	}

接收请求参数序列化 serialize

请求地址: 详见jquery.jsp文件

	// $("#form").serialize() 显示给出?name=baidu&note=mmp
	@RequestMapping(value = "/serialize", method = RequestMethod.GET)
	public ModelAndView serialize(BeanParam beanParm) {
		// 数据域
		ModelAndView mv = new ModelAndView();
		mv.addObject(beanParm);
		mv.setView(new MappingJackson2JsonView());
		return mv;
	}

猜你喜欢

转载自blog.csdn.net/dengjili/article/details/84498732