如果注解加在方法上代表二级目录
@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>
如果注解加在类上,代表以及目录
@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>
当只有一个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>
@Controller
@RequestMapping(path = "/user")
public class HelloController {
@RequestMapping(value = "/testRequestMapping",method = {RequestMethod.POST})
public String testRequestMapping(){
System.out.println("测试RequestMapping注解");
return "success";
}
}
如果只指定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>
@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>
@Controller
@RequestMapping(path = "/user")
public class HelloController {
@RequestMapping(value = "/testRequestMapping",headers = {"Accept"})
public String testRequestMapping(){
System.out.println("测试RequestMapping注解");
return "success";
}
}
![SpringMvc中RequestMapping注解_第7张图片](http://img.e-com-net.com/image/info8/179a43e38f0f4ae0a596df517b7b00bb.jpg)
![SpringMvc中RequestMapping注解_第8张图片](http://img.e-com-net.com/image/info8/f28f4d9ec6be4104b8b0cd4130db4c3b.jpg)
如果没有配置项目路径也没有配置/
,那么就可以不用加/