Spring MVC从入门到精通—@RequestMapping注解详解

       在Spring MVC中@RequestMapping注解是一个十分强大的注解,Spring MVC使用@RequestMapping注解可以为控制器指定可以处理那些URL请求,在控制器的类上或类中的方法上均可以使用这个注解:

  • 在类上使用可以提供初步的映射信息。相当于一个根路径
  • 在方法上使用提供更进一步的细分映射信息。

(1)这是一个只在方法上使用@RequestMapping注解的例子:

package com.xzy.controller;

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

@Controller
public class HelloServlet0 {

    @RequestMapping("/hello")
    public  String sayHello(Model model){
        System.out.println("收到请求,正在处理.......");

        model.addAttribute("msg","This is a Spring MVC Web");
        return "success";
    }
}

(2)这是一个在类和方法同时使用@RequestMapping注解的例子:

package com.xzy.controller;

import com.xzy.bean.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/welcome")   //这个相当于是基准路径
public class HelloServlet {

    @RequestMapping("/hello")
    public String sayHello(Model model){
        System.out.println("收到请求,正在处理......");
        model.addAttribute("msg","Welcome to SpringMVC!");
        return "hello";
    }
}

@Requestmapping的属性:

属性作用
value 默认的属性就是value,他就是一个URL路径,可以在一个方法上或类上给多个value值
method 用来定义接收浏览器发来的何种请求。在Spring中,使用枚举类RequestMethod来封装了HTTP协议的所有请求方式。最基本的有GET、POST、DELETE、PUT
params 表示请求参数,也就是追加在URL上的键值对,多个请求参数以&隔开
headers 该属性表示请求头,通过 @RequestMapping 中的 headers 属性,可以限制客户端发来的请求
consumes 规定请求头的Content-Type
produces 告诉浏览器返回的内容是什么,给响应头中加上Content-Type

示例代码: method:限定请求方法

HelloServlet.java
 /**
     * 指定只有GET请求才可一访问
     * @param model
     * @return
     */
    @RequestMapping(value="/handler1",method= RequestMethod.GET)
    public String handler1(Model model){
        System.out.println("收到请求,正在处理......");

        model.addAttribute("msg","这是一个只有GET请求才可以访问页面");

        return "success";
    }

    /**
     * 指定只有POST请求才可以来访问
     * @param model
     * @return
     */
    @RequestMapping(value = "/handler2",method = RequestMethod.POST)
    public String handler2(Model model){
        System.out.println("收到请求,正在处理......");

        model.addAttribute("msg","这是一个只有POST请求才可以访问页面");

        return "success";
    }
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>@RequestMapping测试</title>
</head>
<body>
<center>
    <a href="hello">1.只在方法方法上用@RequestMapping注解</a><br/>
    <a href="home/hello">2.在类和方法上同时用@RequestMapping注解</a><br/>
    <a href="home/handler1">3.通过GET方法来访问</a><br/>
    <form action="home/handler2" method="post">
        <button type="submit">4.POST提交</button>
    </form>
</center>
</body>
</html>

parmars:限定参数 parmars支持简单的表达式计算,例如: eg1:params = {"username"} ,表示请求的路径中必须要有username这个关键字,如果没有就会报异常

eg2:params = {"!username"} ,表示请求的路径中不能有username这个关键字,如果有这个参数就会报异常 eg2:params = {"username","age=20","pwd"} ,表示请求的路径中必须要有username、age、pwd这三个关键字,并且age必须是20,如果没有或age的值不等就会报异常 Alt text

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>@RequestMapping测试</title>
</head>
<body>
<center>
    <a href="hello">1.只在方法方法上用@RequestMapping注解</a><br/>
    <a href="home/hello">2.在类和方法上同时用@RequestMapping注解</a><br/>
    <a href="home/handler1">3.通过GET方法来访问</a><br/>
    <form action="home/handler2" method="post">
        <button type="submit">4.POST提交</button>
    </form>
    <a href="home/handler3?username=hx">5.限定请求参数1</a><br/>
    <a href="home/handler4?username=hx&age=20&pwd=123456">5.限定请求参数2</a><br/>
</center>
</body>
</html>

headers:限定请求头部 示例一:

测试结果:

留言区

还能输入500个字符