首页 > 其他分享 >spring mvc——@Controller和@RequestMapping注解

spring mvc——@Controller和@RequestMapping注解

时间:2022-12-18 13:45:37浏览次数:41  
标签:控制器 RequestMapping spring value Controller mvc 注解 public

 

 

 

 

 

 

 

 

 

@RequestMapping 注解

@RequestMapping 注解是 Spring MVC 中最常被用到的注解之一。它通常被标注在控制器方法上,负责将请求与处理请求的控制器方法关联起来,建立映射关系。

Spring MVC 的前端控制器(DispatcherServlet)拦截到用户发来的请求后,会通过 @RequestMapping 注解提供的映射信息找到对应的控制器方法,对这个请求进行处理。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package net.biancheng.c.controller;

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

@Controller
@RequestMapping("/springmvc")
public class RequestMappingController {

    /**
     * @author c语言中文网
     * @RequestMapping value、name、method 属性
     */
    @RequestMapping(value = {"/welcome", "/do"}, name = "跳转到欢迎页", method = RequestMethod.GET)
    public String testValue() {
        return "welcome";
    }

    /**
     * @author c语言中文网
     * @RequestMapping 注解 params 属性
     */
    @RequestMapping(value = "/testParam", params = {"name=springmvc", "url=http://c.biancheng.net"})
    public String params() {
        return "param";
    }
   
    /**
     * @author c语言中文网
     * @RequestMapping 注解 headers 属性
     */
    @RequestMapping(value = "/testHeader", headers = {"Content-Type=application/json", "Referer=http://c.biancheng.net"})
    public String headers() {
        return "header";
    }
}

 

标签:控制器,RequestMapping,spring,value,Controller,mvc,注解,public
From: https://www.cnblogs.com/xiaobaibailongma/p/16990283.html

相关文章