聊聊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
- 控制器負責提供訪問應用程序的行為,通常通過接口定義或注解定義兩種方法實現(xiàn)。
- 控制器負責解析用戶的請求并將其轉換為一個模型。
- 在Spring MVC中一個控制器可以包含多個方法
- 在Spring MVC中,對于Controller的配置方式有很多種
實現(xiàn)Controller接口
Controller是一個接口,在org.springframework.web.servlet.mvc包下,接口中只有一個方法
//實現(xiàn)該接口的類獲得控制器功能
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;
//只要實現(xiàn)了 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運行測試!

說明:
實現(xiàn)接口Controller定義控制器是較老的辦法
缺點是:一個控制器中只有一個方法,如果要多個方法則需要定義多個Controller;定義的方式比較麻煩
使用注解@Controller
@Controller注解類型用于聲明Spring類的實例是一個控制器。
Spring可以使用掃描機制來找到應用程序中所有基于注解的控制器類,為了保證Spring能找到你的控制器,需要在配置文件中聲明組件掃描。
<!-- 自動掃描指定的包,下面所有注解類交給IOC容器管理 --> <context:component-scan base-package="com.chen.controller"/>
增加一個ControllerTest2類,使用注解實現(xiàn);
// @Controller代表這個類會被Spring接管,被這個注解的類,中的所有方法,
// 如果返回值是String,并且有具體頁面可以跳轉,那么就會被視圖解析器解析;
@Controller
public class ControllerTest2 {
@RequestMapping("/test2")
public String test1(Model model){
model.addAttribute("msg","ControllerTest2");
return "test";
}
}
運行Tomcat測試

可以發(fā)現(xiàn),我們的兩個請求都可以指向一個視圖,但是頁面的顯示結果是不也一樣的,從這里可以看出視圖是被復用的,而控制器與視圖之間是弱耦合關系
RequestMapping說明
@RequestMapping
- @RequestMapping注解用于映射url到控制器類或一個特定的處理程序方法??捎糜陬惢蚍椒ㄉ?。用于類上,表示類中的所有響應請求的方法都是以該地址作為父路徑。
測試一
@Controller
public class ControllerTest3 {
@RequestMapping("/t1")
public String test(Model model){
model.addAttribute("msg","ControllerTest3");
return "test";
}
}

訪問路徑:http://localhost:8080 / 項目名 / t1
測試二
@Controller
@RequestMapping("/c3")
public class ControllerTest3 {
@RequestMapping("/t1")
public String test(Model model){
model.addAttribute("msg","ControllerTest3");
return "test";
}
}

訪問路徑:http://localhost:8080 / 項目名/ admin /h1 , 需要先指定類的路徑再指定方法的路徑;
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java通過httpclient比較重定向和請求轉發(fā)
這篇文章主要介紹了Java通過httpclient比較重定向和請求轉發(fā),HttpClient?4.x?版本,get請求方法會自動進行重定向,而post請求方法不會自動進行重定向,需要的朋友可以參考下2023-04-04
servlet之web路徑問題_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了servlet之web路徑問題的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Spring?Cloud微服務架構Sentinel數據雙向同步
這篇文章主要為大家介紹了Spring?Cloud微服務架構Sentinel數據雙向同步示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
SpringBoot中的@RestControllerAdvice注解詳解
這篇文章主要介紹了SpringBoot中的@RestControllerAdvice注解詳解,RestControllerAdvice注解用于創(chuàng)建全局異常處理類,用于捕獲和處理整個應用程序中的異常,需要的朋友可以參考下2024-01-01

