文章目录
-
- 1.RequestMapping注解
-
- 1.1 注解加在方法上
- 1.2 注解加在类上
- 2.ResultMapping的属性
-
- 2.1 value属性
- 2.2 Method属性
- 2.3 params属性
- 2.4 headers属性
- 3.路径``
1.RequestMapping注解
- RequestMapping注解的作用是建立请求URL和处理方法之间的关系
- RequestMapping注解可以作用在类上和方法上
作用在类上:第一级的访问目录
作用在方法上:第二级的访问目录
1.1 注解加在方法上
如果注解加在方法上代表二级目录
@Controller
public class HelloController {
@RequestMapping(path = "/testRequestMapping")
public String testRequestMapping(){
System.out.println("测试RequestMapping注解");
return "success";
}
}
那么请求路径为:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="/testRequestMapping">RequestMapping注解</a>
</body>
</html>
1.2 注解加在类上
如果注解加在类上,代表以及目录
@Controller
@RequestMapping(path = "/user")
public class HelloController {
@RequestMapping(path = "/testRequestMapping")
public String testRequestMapping(){
System.out.println("测试RequestMapping注解");
return "success";
}
}
那么请求路径为:
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="/user/testRequestMapping">RequestMapping注解</a>
</body>
</html>
2.ResultMapping的属性
- path:指定请求路径的URL
- value:value属性和path属性是一样的
- method:指定该方法请求方式
- params:指定限制请求参数的条件
- headers:发送的请求中必须包含的请求头
2.1 value属性
当只有一个value和path属性时,关键字可以省略:
@Controller
@RequestMapping(path = "/user")
public class HelloController {
@RequestMapping(value = "/testRequestMapping")
public String testRequestMapping(){
System.out.println("测试RequestMapping注解");
return "success";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="/user/testRequestMapping">RequestMapping注解</a>
</body>
</html>
2.2 Method属性
@Controller
@RequestMapping(path = "/user")
public class HelloController {
@RequestMapping(value = "/testRequestMapping",method = {RequestMethod.POST})
public String testRequestMapping(){
System.out.println("测试RequestMapping注解");
return "success";
}
}
2.3 params属性
如果只指定username属性
@Controller
@RequestMapping(path = "/user")
public class HelloController {
@RequestMapping(value = "/testRequestMapping",params = {"username"})
public String testRequestMapping(){
System.out.println("测试RequestMapping注解");
return "success";
}
}
那么请求路径为中username的属性值可以为任意的
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="/user/testRequestMapping?username=ghh">RequestMapping注解</a>
</body>
</html>
如果指定username的属性值
@Controller
@RequestMapping(path = "/user")
public class HelloController {
@RequestMapping(value = "/testRequestMapping",params = {"username=haha"})
public String testRequestMapping(){
System.out.println("测试RequestMapping注解");
return "success";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="/user/testRequestMapping?username=haha">RequestMapping注解</a>
</body>
</html>
2.4 headers属性
@Controller
@RequestMapping(path = "/user")
public class HelloController {
@RequestMapping(value = "/testRequestMapping",headers = {"Accept"})
public String testRequestMapping(){
System.out.println("测试RequestMapping注解");
return "success";
}
}
3.路径<a href="/user/testRequestMapping>
又得同学说加了/
报错:
正常情况下,如果Tomcat配置了项目路径,那么就要加/
如果没有配置项目路径也没有配置/
,那么就可以不用加/