SpringMvc中RequestMapping注解

文章目录

    • 1.RequestMapping注解
      • 1.1 注解加在方法上
      • 1.2 注解加在类上
    • 2.ResultMapping的属性
      • 2.1 value属性
      • 2.2 Method属性
      • 2.3 params属性
      • 2.4 headers属性
    • 3.路径``

1.RequestMapping注解

  1. RequestMapping注解的作用是建立请求URL和处理方法之间的关系
  2. 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>

SpringMvc中RequestMapping注解_第1张图片

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>

SpringMvc中RequestMapping注解_第2张图片

2.ResultMapping的属性

  1. path:指定请求路径的URL
  2. value:value属性和path属性是一样的
  3. method:指定该方法请求方式
  4. params:指定限制请求参数的条件
  5. 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";
    }
}

SpringMvc中RequestMapping注解_第3张图片

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>

SpringMvc中RequestMapping注解_第4张图片
如果指定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>

SpringMvc中RequestMapping注解_第5张图片

2.4 headers属性

@Controller
@RequestMapping(path = "/user")
public class HelloController {
    @RequestMapping(value = "/testRequestMapping",headers = {"Accept"})
    public String testRequestMapping(){
        System.out.println("测试RequestMapping注解");
        return "success";
    }
}

SpringMvc中RequestMapping注解_第6张图片

3.路径SpringMvc中RequestMapping注解_第7张图片
SpringMvc中RequestMapping注解_第8张图片
如果没有配置项目路径也没有配置/,那么就可以不用加/
SpringMvc中RequestMapping注解_第9张图片

你可能感兴趣的:(SpringMvc)