1. 概述
本文介绍了spring mvc中使用@RequestMapping时,支持常用方法参数类型和返回类型。
常用的方法参数类型有:
- 1 PathVariable
- 2 RequestParam
- 3 RequestBody
- 4 HttpEntity
- 5 CookieValue
- 6 RequestHeader
- 7 自动封装form表单请求到对象中
- 8 HttpServletRequest HttpServletResponse
- 9 RequestMapping 参数配置params headers
常用的返回类型有:
- 1 返回一个页面的地址
- 2 ResponseBody
- 3 ResponseEntity
- 4 ModelAndView
2. 前提条件
代码工程名称:mvc
测试PO类
ModelAttributeVO
public class ModelAttributeVO {
private String name;
private String value;
private Date date;
// set/get方法略
}
VO
public class VO {
private String name;
private String value;
private Date date;
// set/get方法略
}
3. @RequestMapping支持的方法参数类型
3.1. RequestParameterController
以下代码都在RequestParameterController类中
@Controller: 表示此类对外提供url服务
@RequestMapping:此注解不仅可以作用在方法上,也可以作用在类上。如果作用在类上,则表示此值是类中的所有@RequestMapping方法的URL的前缀
@Controller
@RequestMapping(value = "/request") // 全局URL
public class RequestParameterController {
....
}
3.2. 使用的jsp
下面用到jsp的页面如下,都在META-INF\resources\WEB-INF\page\reqparameter目录下:
showInput.jsp
打印内容
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Request Parameter</title>
</head>
<body>
${map}
</body>
</html>
formModel.jsp
测试form表单
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="myform" method="post" action="formModel">
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="name" value="fisr name" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="value" value="lastName" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Save Changes" />
</td>
</tr>
</table>
</form>
</body>
</html>
httpEntityForm.jsp
测试form表单
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="myform" method="post" action="httpEntity">
<table>
<tr>
<td>First Name:</td>
<td><input type="text" value="name" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><<