Spring MVC 常用注解
@RequestMapping
Spring MVC 通过 @RequestMapping 注解将 URL 请求与业务方法进行映射,在控制器的类定义处以及方法定义处都可以添加 @RequestMapping ,在类定义处添加相当于多了⼀层访问路径。
package com.southwind.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloHandler {
@RequestMapping("/index")
public String index(){
System.out.println("接收到了请求");
//返回逻辑视图
return "index";
}
}
URI:http://localhost:8080/hello/index
@RequestMapping 常用参数
value
value:指定 URL 请求的实际地址,是 @RequestMapping 的默认值
@RequestMapping("/index")
public String index(){
System.out.println("接收到了请求");
//返回逻辑视图
return "index"; }
等同于
@RequestMapping(value="/index")
public String index(){
System.out.println("接收到了请求");
//返回逻辑视图
return "index"; }
method
method:指定请求的 method 类型,包括 GET、POST、PUT、DELETE 等。
@RequestMapping(value = "/index",method = RequestMethod.POST)
public String index(){
System.out.println("接收到了请求");
//返回逻辑视图
return "index"; }
上述代码表示只有 POST 请求可以访问该方法,若使用其他请求访问,直接抛出异常,比如 Get。
params
params:指定 request 请求中必须包含的参数值,若不包含,无法调用该方法。
@RequestMapping(value = "/index",method = RequestMethod.POST,params = {"id=1","name=tom"})
public String index(){
System.out.println("接收到了请求");
//返回逻辑视图
return "index"; }
上述代码表示 request 请求中必须包含 name 和 id 两个参数,并且 id 的值必须为 1,name 的值必须为 tom,才可调⽤,否则抛出 400 异常。
参数绑定
params 是对 URL 请求参数进行限制,不满足条件的 URL 无法访问该方法,需要在业务方法中获取URL 的参数值。
1、在业务方法定义时声明参数列表。
2、给参数列表添加 @RequestParam 注解进行绑定。
@RequestMapping(value = "/index",method = RequestMethod.POST)
public String index(@RequestParam("num") Integer id,@RequestParam("str")
String name){
System.out.println("接收到了请求,参数是:id="+id+",name="+name);
//返回逻辑视图
return "index"; }
Spring MVC 可以自动完成数据类型转换,该工作是由 HandlerAdapter 来完成的。
Spring MVC 也⽀持 RESTful 风格的 URL 参数获取
传统的 URL:localhost:8080/hello/index?id=1&name=tom
RESTful URL:localhost:8080/hello/index/1/tom
@RequestMapping("/restful/{id}/{name}")
public String restful(@PathVariable("id") Integer id,@PathVariable("name")
String name){
System.out.println(id+"-"+name);
return "index"; }
将参数列表的注解改为 @PathVariable(“id”) 即可。
映射 Cookie
@RequestMapping("/cookie")
public String getCookie(@CookieValue("JSESSIONID") String sessionId){
System.out.println(sessionId);
return "index"; }
使用POJO 绑定参数
Spring MVC 会根据请求参数名和 POJO 属性名进⾏匹配,自动为该对象填充属性值,并且支持属性级
联。
如果出现中⽂乱码,可以通过配置过滤器来解决,在 web.xml 中添加配置即可。
<filter>
<filter-name>encodingFilter</filter-name>
<filterclass>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Address
package com.southwind.entity;
import lombok.Data;
@Data
public class Address {
private Integer code;
private String value; }
User
package com.southwind.entity;
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private Address address; }
addUser.jsp
<%--
Created by IntelliJ IDEA.
User: southwind
Date: 2020-02-07
Time: 16:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html> <head>
<title>Title</title>
</head> <body>
<form action="/hello/add" method="post">
<table>
<tr>
<td>编号:</td>
<td>
<input type="text" name="id"/>
</td>
</tr>
<tr>
<td>姓名:</td>
<td>
<input type="text" name="name"/>
</td>
</tr>
<tr>
<td>地址编号:</td>
<td>
<input type="text" name="address.code"/>
</td>
</tr>
<tr>
<td>地址信息:</td>
<td>
<input type="text" name="address.value"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
</body>
</html>
Handler
@RequestMapping(value = "/add",method = RequestMethod.POST)
public String add(User user){
System.out.println(user);
return "index"; }
主体对象可以没有无参构造函数,但是级联对象必须有无参构造函数