聊聊Controller中RequestMapping的作用

Controller @RequestMapping作用

@RequestMapping是一个用来处理请求地址映射的注解,可用于类或者方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

@RequestMapping注解有六个属性,下面进行详细的说明。

1.value, method

  • value:指定请求的实际地址,指定的地址可以是URI Template模式。
  • method:指定请求的method类型,GET、POST、PUT、DELETE等。

2.consumes, produces

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

3.params, headers

  • params:指定request中必须包含某些参数值才让该方法处理。
  • headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。

其实还可以简单的认为这个注解就是使用在controller类上面的注解

如果在controller类上面添加了注解@RequestMapping("/product") ,然后又在方法上面加上了注解@RequestMapping("/findAll"),你的项目端口是8080,然后在访问的时候就是localhost:8080/product/findAll

Controller配置总结及RequestMapping说明

控制器Controller

  • 控制器负责提供访问应用程序的行为,通常通过接口定义或注解定义两种方法实现。
  • 控制器负责解析用户的请求并将其转换为一个模型。
  • 在Spring MVC中一个控制器可以包含多个方法
  • 在Spring MVC中,对于Controller的配置方式有很多种

实现Controller接口

Controller是一个接口,在org.springframework.web.servlet.mvc包下,接口中只有一个方法

//实现该接口的类获得控制器功能
public interface Controller {
   //处理请求且返回一个模型与视图对象
   ModelAndView handleRequest(HttpServletRequest var1, HttpServletResponse var2) throws Exception;
}

测试

1.新建一个Moudle,springmvc-04-controller。

2.编写web,xml



    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springmvc-servlet.xml
        
        1
    

    springmvc
    /


3.编写springmvc-servlet.xml



    
    
    
    
    
        
        
        
        
    

4.编写一个Controller类,ControllerTest1

package com.chen.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//只要实现了 Controller 接口的类,说明这就是一个控制器了
public class ControllerTest1 implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","ControllerTest1");
        mv.setViewName("test");
        return mv;
    }
}

编写完毕后,去Spring配置文件中注册请求的bean;name对应请求路径,class对应处理该请求的类

编写前端test.jsp,注意在WEB-INF/jsp目录下编写,对应我们的视图解析器

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


${msg}

配置Tomcat运行测试!

聊聊Controller中RequestMapping的作用_第1张图片

说明:

实现接口Controller定义控制器是较老的办法

缺点是:一个控制器中只有一个方法,如果要多个方法则需要定义多个Controller;定义的方式比较麻烦

使用注解@Controller

@Controller注解类型用于声明Spring类的实例是一个控制器。

Spring可以使用扫描机制来找到应用程序中所有基于注解的控制器类,为了保证Spring能找到你的控制器,需要在配置文件中声明组件扫描。


增加一个ControllerTest2类,使用注解实现;

// @Controller代表这个类会被Spring接管,被这个注解的类,中的所有方法,
// 如果返回值是String,并且有具体页面可以跳转,那么就会被视图解析器解析;
@Controller
public class ControllerTest2 {
    @RequestMapping("/test2")
    public String test1(Model model){
        model.addAttribute("msg","ControllerTest2");
        return "test";
    }
}

运行Tomcat测试

聊聊Controller中RequestMapping的作用_第2张图片

可以发现,我们的两个请求都可以指向一个视图,但是页面的显示结果是不也一样的,从这里可以看出视图是被复用的,而控制器与视图之间是弱耦合关系

RequestMapping说明

@RequestMapping

  • @RequestMapping注解用于映射url到控制器类或一个特定的处理程序方法。可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

测试一

@Controller
public class ControllerTest3 {
    @RequestMapping("/t1")
    public String test(Model model){
        model.addAttribute("msg","ControllerTest3");
        return "test";
    }
}

聊聊Controller中RequestMapping的作用_第3张图片

访问路径:http://localhost:8080 / 项目名 / t1

测试二

@Controller
@RequestMapping("/c3")
public class ControllerTest3 {
    @RequestMapping("/t1")
    public String test(Model model){
        model.addAttribute("msg","ControllerTest3");
        return "test";
    }
}

聊聊Controller中RequestMapping的作用_第4张图片

访问路径:http://localhost:8080 / 项目名/ admin /h1 , 需要先指定类的路径再指定方法的路径;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(聊聊Controller中RequestMapping的作用)