软件编程
位置:首页>> 软件编程>> java编程>> 聊聊Controller中RequestMapping的作用

聊聊Controller中RequestMapping的作用

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

标签: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

<?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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com