SpringMVC(二)@RequestMapping注解

@RequestMapping注解

1、@RequestMapping注解的功能

从注解名称上我们可以看到,@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系。

SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。

@RequestMapping请求的映射地址只能存在一个,否则报错!

2、@RequestMapping注解的位置

@RequestMapping标识一个类:设置映射请求的请求路径的初始信息

@RequestMapping标识一个方法:设置映射请求请求路径的具体信息

当不同模块中的请求@RequestMapping值是同一个名字时,既不想修改最具表达此时功能的名字,又不想让处理器报错,就可以分别在页面请求上面加一层路径,controller类上添加@RequestMapping(“/xxxx),用于区分。

案例:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/hello/testRequestMapping}">测试testRequestMapping</a>
</body>
</html>
@Controller
@RequestMapping("hello")
public class RequestMappingController {
	//此时请求映射所映射的请求的请求路径为:/hello/testRequestMapping(即上面网页中的请求)
    @RequestMapping("/testRequestMapping")
    public String success1(){
        return "success";
    }
}

3、@RequestMapping注解的value属性

@RequestMapping注解的value属性通过请求的请求地址匹配请求映射

@RequestMapping注解的value属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址所对应的请求(只要满足一个即可)

@RequestMapping注解的value属性必须设置,至少通过请求地址匹配请求映射

实例多个请求地址:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/hello/testRequestMapping}">测试testRequestMapping</a><br>
<a th:href="@{/hello/testRequestMapping}">测试testRequestMapping-value属性</a><br>
<a th:href="@{/hello/test}">测试test</a><br>
</body>
</html>
@Controller
@RequestMapping("hello")
public class RequestMappingController {
	//此时请求映射所映射的请求的请求路径为:/hello/testRequestMapping(即上面网页中的请求)
    @RequestMapping(value = {"/testRequestMapping","/test"})
	public String success(){
    	return "success";
	}
}

4、@RequestMapping注解的method属性

@RequestMapping注解的method属性通过请求的请求方式(get或post)匹配请求映射(在没有声明method方法时,method默认所有的请求方式都可以匹配,一旦声明了,只能匹配声明的请求方式)。

@RequestMapping注解的method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配多种请求方式的请求,若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错405:Request method ‘POST’ not supported

示例:

<form th:action="@{/requestMappingMethod}" method="post">
    <input type="submit" value="测试RequestMappingMethod属性-->post"/>
</form><br>
<form th:action="@{/requestMappingMethod}" method="get">
    <input type="submit" value="测试RequestMappingMethod属性-->get"/>
</form><br>
@Controller
public class RequestMappingController {
    @RequestMapping(
            value = {"requestMappingMethod"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    public String requestMappingMethod() {
        return "success";
    }
}

注:

1、对于处理指定请求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解,使用这样的注解可省略method的方法指定,只需要赋value值。

处理get请求的映射–>@GetMapping

处理post请求的映射–>@PostMapping

处理put请求的映射–>@PutMapping

处理delete请求的映射–>@DeleteMapping

2、常用的请求方式有get,post,put,delete

但是目前浏览器只支持get和post,若在form表单提交时,为method设置了其他请求方式的字符串(put或delete),则按照默认的请求方式get处理。 若要发送put和delete请求,则需要通过spring提供的过滤器HiddenHttpMethodFilter,在RESTful部分会讲到

5、@RequestMapping注解的params属性(了解)

@RequestMapping注解的params属性通过请求的请求参数匹配请求映射

@RequestMapping注解的params属性是一个字符串类型的数组,可以通过四种表达式设置请求参数和请求映射的匹配关系(只要声明了在数组中的条件,都要满足才能匹配)

“param”:要求请求映射所匹配的请求必须携带param请求参数

“!param”:要求请求映射所匹配的请求必须不能携带param请求参数

“param=value”:要求请求映射所匹配的请求必须携带param请求参数且param=value

“param!=value”:要求请求映射所匹配的请求必须携带param请求参数但是param!=value

<form th:action="@{/requestMappingParams}" method="post">
    用户名:<input type="text" name="username" value="张三"/><br>
    密 码:<input type="password" name="password" value="123456"/><br>
    <input type="submit" value="测试@RequestMapping的params属性-->post"/>
</form><br>

或者

<a th:href="@{/requestMappingParams(username='张三',password=123456)}">测试@RequestMapping的params属性-->get</a><br>

RequestMappingController

@Controller
public class RequestMappingController {
    @RequestMapping(
            value = {"requestMappingParams"},
            method = {RequestMethod.GET, RequestMethod.POST},
            //username表示参数必须携带username,对username的值没有要求
            //password=123456表示参数必须携带password,且值必须是123456
            params = {"username","password=123456"}
    )
    public String requestMappingParams() {
        return "success";
    }
}

注:

若当前请求满足@RequestMapping注解的value和method属性,但是不满足params属性,此时页面回报错400:Parameter conditions “username, password!=123456” not met for actual request parameters: username={admin}, password={123456}

6、@RequestMapping注解的headers属性(了解)

@RequestMapping注解的headers属性通过请求的请求头信息匹配请求映射

@RequestMapping注解的headers属性是一个字符串类型的数组,可以通过四种表达式设置请求头信息和请求映射的匹配关系(只要声明了在数组中的条件,都要满足才能匹配)

“header”:要求请求映射所匹配的请求必须携带header请求头信息

“!header”:要求请求映射所匹配的请求必须不能携带header请求头信息

“header=value”:要求请求映射所匹配的请求必须携带header请求头信息且header=value

“header!=value”:要求请求映射所匹配的请求必须携带header请求头信息且header!=value

若当前请求满足@RequestMapping注解的value和method属性,但是不满足headers属性,此时页面显示404错误,即资源未找到

<a th:href="@{/requestMappingHeader}">测试@RequestMapping的Header属性</a><br>
@Controller
public class RequestMappingController {
    @RequestMapping(
            value = {"/requestMappingHeader"},
            //请求头中必须含有Host,且值要为localhost:8080,否则报错
            headers = {"Host=localhost:8080"}
    )
    public String requestMappingHeader() {
        return "success";
    }
}

7、SpringMVC支持ant风格的路径(模糊匹配)

?:表示任意的单个字符(不支持斜杆/和?,必须要有一个字符)

*:表示任意的0个或多个字符(不支持斜杆/和?)

**:表示任意的一层或多层目录(不支持斜杆?)

注意:在使用**时,只能使用/**/xxx的方式

a测试?匹配任意单个字符

<a th:href="@{/a1a/anySingleCharacter}">测试?匹配任意单个字符</a><br>
@RequestMapping("/a?a/anySingleCharacter" )
    public String anySingleCharacter() {
        return "success";
    }

b测试*匹配任意0个或多个字符

<a th:href="@{/abbbbba/anyMoreCharacter}">测试*匹配任意0个或多个字符</a><br>
@RequestMapping("/a*a/anyMoreCharacter" )
    public String anyMoreCharacter() {
        return "success";
    }

c测试**匹配任意层

<a th:href="@{/aa/bb/cc/anyMoreLayer}">测试**匹配任意层</a><br>
@RequestMapping("/**/anyMoreLayer" )
    public String anyMoreLayer() {
        return "success";
    }

8、SpringMVC支持路径中的占位符(重点)

原始方式:/deleteUser?id=1

rest方式:/deleteUser/1

SpringMVC路径中的占位符常用于RESTful风格中,当请求路径中将某些数据通过路径的方式传输到服务器中,就可以在相应的@RequestMapping注解的value属性中通过占位符{xxx}表示传输的数据,在通过@PathVariable注解,将占位符所表示的数据赋值给控制器方法的形参

<a th:href="@{/placeholder/1/admin}">测试路径中的占位符</a><br>
@RequestMapping("/placeholder/{id}/{username}")
    public String placeholder(@PathVariable("id") String id, @PathVariable("username") String username){
        System.out.println("id:"+id+",username:"+username);
        return "success";
    }
//最终输出的内容为-->id:1,username:admin
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值