聊聊Controller中RequestMapping的作用
Controller @RequestMapping作用
@RequestMapping是一個(gè)用來處理請(qǐng)求地址映射的注解,可用于類或者方法上。用于類上,表示類中的所有響應(yīng)請(qǐng)求的方法都是以該地址作為父路徑。
@RequestMapping注解有六個(gè)屬性,下面進(jìn)行詳細(xì)的說明。
1.value, method
value:指定請(qǐng)求的實(shí)際地址,指定的地址可以是URI Template模式。method:指定請(qǐng)求的method類型,GET、POST、PUT、DELETE等。
2.consumes, produces
consumes:指定處理請(qǐng)求的提交內(nèi)容類型(Content-Type),例如application/json,text/html。produces:指定返回的內(nèi)容類型,僅當(dāng)request請(qǐng)求頭中的(Accept)類型中包含該指定類型才返回。
3.params, headers
params:指定request中必須包含某些參數(shù)值才讓該方法處理。headers:指定request中必須包含某些指定的header值,才能讓該方法處理請(qǐng)求。
其實(shí)還可以簡(jiǎn)單的認(rèn)為這個(gè)注解就是使用在controller類上面的注解
如果在controller類上面添加了注解@RequestMapping("/product") ,然后又在方法上面加上了注解@RequestMapping("/findAll"),你的項(xiàng)目端口是8080,然后在訪問的時(shí)候就是localhost:8080/product/findAll
Controller配置總結(jié)及RequestMapping說明
控制器Controller
- 控制器負(fù)責(zé)提供訪問應(yīng)用程序的行為,通常通過接口定義或注解定義兩種方法實(shí)現(xiàn)。
- 控制器負(fù)責(zé)解析用戶的請(qǐng)求并將其轉(zhuǎn)換為一個(gè)模型。
- 在Spring MVC中一個(gè)控制器可以包含多個(gè)方法
- 在Spring MVC中,對(duì)于Controller的配置方式有很多種
實(shí)現(xiàn)Controller接口
Controller是一個(gè)接口,在org.springframework.web.servlet.mvc包下,接口中只有一個(gè)方法
//實(shí)現(xiàn)該接口的類獲得控制器功能
public interface Controller {
//處理請(qǐng)求且返回一個(gè)模型與視圖對(duì)象
ModelAndView handleRequest(HttpServletRequest var1, HttpServletResponse var2) throws Exception;
}
測(cè)試
1.新建一個(gè)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.編寫一個(gè)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;
//只要實(shí)現(xiàn)了 Controller 接口的類,說明這就是一個(gè)控制器了
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配置文件中注冊(cè)請(qǐng)求的bean;name對(duì)應(yīng)請(qǐng)求路徑,class對(duì)應(yīng)處理該請(qǐng)求的類
<bean name="/test1" class="com.chen.controller.ControllerTest1"/>
編寫前端test.jsp,注意在WEB-INF/jsp目錄下編寫,對(duì)應(yīng)我們的視圖解析器
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
? ? <title>Title</title>
</head>
<body>
${msg}
</body>
</html>配置Tomcat運(yùn)行測(cè)試!

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

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

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

訪問路徑:http://localhost:8080 / 項(xiàng)目名/ admin /h1 , 需要先指定類的路徑再指定方法的路徑;
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatisPlus中事務(wù)處理的實(shí)現(xiàn)
本文主要介紹了MyBatisPlus中事務(wù)處理的實(shí)現(xiàn),包括事務(wù)的開啟、提交、回滾等操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
深入學(xué)習(xí)Java單元測(cè)試(Junit+Mock+代碼覆蓋率)
在做單元測(cè)試時(shí),代碼覆蓋率常常被拿來作為衡量測(cè)試好壞的指標(biāo),甚至,用代碼覆蓋率來考核測(cè)試任務(wù)完成情況,比如,代碼覆蓋率必須達(dá)到80%或 90%。下面我們就來詳細(xì)學(xué)習(xí)下java單元測(cè)試吧2019-06-06
java 日志的數(shù)據(jù)脫敏的實(shí)現(xiàn)方法
今日給大家介紹一下java 日志的數(shù)據(jù)脫敏的實(shí)現(xiàn)方法,可以更好的保護(hù)數(shù)據(jù)的安全,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
springboot中Getmapping獲取參數(shù)的實(shí)現(xiàn)方式
這篇文章主要介紹了springboot中Getmapping獲取參數(shù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Springboot @Transactional使用時(shí)需注意的幾個(gè)問題記錄
本文詳細(xì)介紹了Spring Boot中使用`@Transactional`注解進(jìn)行事務(wù)管理的多個(gè)方面,包括事務(wù)的隔離級(jí)別(如REPEATABLE_READ)和傳播行為(如REQUIRES_NEW),并指出了在同一個(gè)類中調(diào)用事務(wù)方法時(shí)可能遇到的問題以及解決方案,感興趣的朋友跟隨小編一起看看吧2025-01-01
idea中MavenWeb項(xiàng)目不能創(chuàng)建Servlet的解決方案
這篇文章主要介紹了idea中MavenWeb項(xiàng)目不能創(chuàng)建Servlet的解決方案,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
MyBatis編寫一個(gè)簡(jiǎn)單的SQL生成工具
MyBatis 是一個(gè)強(qiáng)大的數(shù)據(jù)持久化框架,它提供了一種半自動(dòng)化的 ORM 實(shí)現(xiàn)方式,本文將為大家介紹如何使用MyBatis編寫一個(gè)簡(jiǎn)單的SQL生成工具,需要的可以了解下2025-03-03

