聊聊Controller中RequestMapping的作用

作者:程序员还要写代码 时间:2021-12-08 20:48:45 

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
        version="4.0">
   <!-- 配置DispatcherServlet -->
   <servlet>
       <servlet-name>springmvc</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:springmvc-servlet.xml</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
   </servlet>
<servlet-mapping>
   <servlet-name>springmvc</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

3.编写springmvc-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
      https://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/mvc
      https://www.springframework.org/schema/mvc/spring-mvc.xsd">
   <context:component-scan base-package="com.chen.controller"/>
   <mvc:annotation-driven/>
   <mvc:default-servlet-handler/>
   <!-- 视图解析器: 模板引擎 Thymeleaf Freemarker -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <!-- 前缀 -->
       <property name="prefix" value="/WEB-INF/jsp/"/>
       <!-- 后缀 -->
       <property name="suffix" value=".jsp"/>
   </bean>
</beans>

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对应处理该请求的类

<bean name="/test1" class="com.chen.controller.ControllerTest1"/>

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

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

配置Tomcat运行测试!

聊聊Controller中RequestMapping的作用

说明:

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

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

使用注解@Controller

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

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

<!-- 自动扫描指定的包,下面所有注解类交给IOC容器管理 -->
<context:component-scan base-package="com.chen.controller"/>

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

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

运行Tomcat测试

聊聊Controller中RequestMapping的作用

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

RequestMapping说明

@RequestMapping

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

测试一

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

聊聊Controller中RequestMapping的作用

访问路径: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的作用

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

来源:https://blog.csdn.net/qq_48642405/article/details/122121523

标签:Controller,RequestMapping
0
投稿

猜你喜欢

  • Java设计模式之备忘录模式实现对象状态的保存和恢复

    2023-08-28 15:22:27
  • js 交互在Flutter 中使用 webview_flutter

    2023-07-20 22:40:14
  • springboot 定时任务@Scheduled实现解析

    2023-11-10 16:18:24
  • Spring Boot 工程的创建和运行(图文)

    2022-06-26 15:39:49
  • 分享几个Java工作中实用的代码优化技巧

    2023-11-28 12:04:50
  • Java中Function的使用及说明

    2023-08-12 03:04:29
  • SpringBoot 使用 FTP 操作文件的过程(删除、上传、下载文件)

    2021-07-26 10:40:05
  • Java overload和override的区别分析

    2023-10-12 15:16:17
  • 带你了解Java的类和对象

    2022-05-08 09:10:21
  • java连接SQL Server数据库的超详细教程

    2023-04-05 21:46:22
  • JAVA中的Token 基于Token的身份验证实例

    2023-11-09 18:05:09
  • springboot+springmvc+mybatis项目整合

    2023-07-16 03:13:57
  • 使用Java实现简单串口通信

    2022-07-27 14:48:52
  • Spring Boot @Conditional注解用法示例介绍

    2023-04-18 22:51:51
  • springboot如何使用logback-spring配置日志格式,并分环境配置

    2023-11-10 04:37:34
  • 为什么Java开发需要配置环境变量

    2023-08-13 13:11:03
  • Java基础:流Stream详解

    2023-11-29 06:11:14
  • Kafka Producer中的消息缓存模型图解详解

    2022-05-03 06:00:13
  • SpringMVC之@requestBody的作用及说明

    2022-06-08 12:35:04
  • java多次嵌套循环查询数据库导致代码中数据处理慢的解决

    2023-10-28 22:17:50
  • asp之家 软件编程 m.aspxhome.com