Spring MVC @RequestMapping注解

版权声明:最终解释权归属Hern、HernSong(hernsong)、苍鹭、www.hernsong.com所有! https://blog.csdn.net/qq_36761831/article/details/88789101

简介

@RequestMapping注解是一个用来处理请求地址映射的注解,相当于Servlet中在web.xml中配置的映射作用一致,在控制器的类定义及方法定义处都可以标注。

用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

DispatcherServlet截获请求后,通过控制器上@RequestMapping提供的映射信息确定请求所对应的处理方法。

@RequestMapping支持Ant风格URL

?:匹配文件名中的一个字符

* :匹配文件名中的任意字符

** :匹配多层路径 

例如:

package com.helloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/helloworld_mvc")
public class HelloWorld {

    @RequestMapping("/*/testAnt")
    public String testAnt(){
        return "success";
    }
}

 注意:index.jsp中 a 标签中的mvcaa为随便写的,映射方法中的 * 号。

<%--
  Created by IntelliJ IDEA.
  User: 23369
  Date: 2019/3/24
  Time: 18:29
  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>
    <a href="helloworld_mvc/mvcaa/testAnt">ant测试</a>
  </body>
</html>

RequestMapping注解的六个属性

注意:若同时定义多个属性,他们之间是与的关系,联合使用多个条件可以让请求映射更加精确化。

1、 value, method

例子: 

  • value:     指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明)。value的uri值为以下三类:可以指定为普通的具体值、 可以指定为含有某变量的一类值(URI Template Patterns with Path Variables)、 可以指定为含正则表达式的一类值( URI Template Patterns with Regular Expressions);
  • method:  指定请求的method类型, GET、POST、PUT、DELETE等。
  • <%--
      Created by IntelliJ IDEA.
      User: 23369
      Date: 2019/3/24
      Time: 18:29
      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="helloworld_mvc/testMethod" method="post">
          <input type="submit" value="测试指定method和value属性">
        </form>
      </body>
    </html>
    
    package com.helloworld;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    @RequestMapping("/helloworld_mvc")
    public class HelloWorld {
    
        @RequestMapping(value = "/testMethod", method = RequestMethod.POST)
        public String testMethod(){
            return "success";
        }
    
    }
    

2、 consumes,produces

  • consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html。
  • produces:    指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回。

3、 params,headers

  • params: 指定request中必须包含某些参数值是,才让该方法处理。
  • headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
<%--
  Created by IntelliJ IDEA.
  User: 23369
  Date: 2019/3/24
  Time: 18:29
  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>

    <a href="helloworld_mvc/testParmter?username=hern&age=11">Params测试</a>
    
  </body>
</html>
package com.helloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/helloworld_mvc")
public class HelloWorld {

    @RequestMapping(value = "testParmter", params = {"username","age!=10"})
    public String testParams(){
        return "success";
    }

}

例子:

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: 23369
  Date: 2019/3/24
  Time: 18:29
  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>
  <a href="hello2">方法定义@RequestMapping</a>
  <br>
  <a href="helloworld_mvc/hello">类定义@RequestMapping</a>
  </body>
</html>

success.jsp

<%--
  Created by IntelliJ IDEA.
  User: 23369
  Date: 2019/3/24
  Time: 18:33
  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>
<h1>success</h1>
</body>
</html>

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
">

    <context:component-scan base-package="com.helloworld"></context:component-scan>

    <mvc:default-servlet-handler></mvc:default-servlet-handler>

    <mvc:annotation-driven></mvc:annotation-driven>

    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

类处定义

提供初步的请求映射信息,相对于WEB应用的根目录。

package com.helloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/helloworld_mvc")
public class HelloWorld {

    @RequestMapping("/hello")
    public String hello(){
        System.out.println("成功");
        return "success";
    }
}

方法处定义

提供进一步的细分映射信息,相对于类定义处的URL,若类定义处未标注@RequestMapping,则方法处标记的URL相对于WEB应用的根目录。

package com.helloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorld2 {

    @RequestMapping("/hello2")
    public String hello(){
        return "success";
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36761831/article/details/88789101
今日推荐