SpringMVC知識盤點及總結2@RequestMapping註解功能
阿新 • • 發佈:2021-12-21
學習目標:@RequestMapping註解的功能
1.建立maven專案,在pom檔案中引入以下依賴:
spring-webmvc
logback-classic
javax.servlet-api
thymeleaf-spring5
2.在web.xml檔案中註冊前端控制器DispatcherServlet
3.在web.xml中配置springMVC配置檔案的位置和名稱
4.將前端控制器DispatcherServlet的初始化時間提前到伺服器啟動時
5.設定springMVC核心控制器所能處理請求的請求路徑。
web.xml完整程式碼:
1 <?xml version="1.0" encoding="UTF-8"?> 2<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 5 version="4.0"> 6 <!--1.註冊前端控制器DispatcherServlet--> 7<servlet> 8 <servlet-name>DispatcherServlet</servlet-name> 9 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 10 <!--2.配置springMVC配置檔案的位置和名稱--> 11 <init-param> 12 <param-name>contextConfigLocation</param-name> 13<param-value>classpath:springMVC.xml</param-value> 14 </init-param> 15 <!--3.將前端控制器DispatcherServlet的初始化時間提前到伺服器啟動時--> 16 <load-on-startup>1</load-on-startup> 17 </servlet> 18 <!--4.設定springMVC核心控制器所能處理請求的請求路徑。 /所匹配的請求 19 可以是/login或.html或.js或.css方式的請求路徑,但是/不能匹配.jsp請求路徑的請求--> 20 <servlet-mapping> 21 <servlet-name>DispatcherServlet</servlet-name> 22 <url-pattern>/</url-pattern> 23 </servlet-mapping> 24 </web-app>
6.在resources下建立已經起好名字的springMVC.xml檔案
7.在springMVC.xml檔案中配置元件掃描器,以及檢視解析器(記得加名稱空間,不然會報500錯誤)
springMVC完整程式碼:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.2.xsd"> 9 10 <!--1.掃描元件--> 11 <context:component-scan base-package="com.st.springmvc.controller"></context:component-scan> 12 <!--2.配置Thymeleaf檢視解析器--> 13 <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> 14 <property name="order" value="1"/> 15 <property name="characterEncoding" value="UTF-8"/> 16 <property name="templateEngine"> 17 <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> 18 <property name="templateResolver"> 19 <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> 20 <!--檢視字首--> 21 <property name="prefix" value="/WEB-INF/templates/"/> 22 <!--檢視字尾--> 23 <property name="suffix" value=".html"/> 24 <property name="templateMode" value="HTML5"/> 25 <property name="characterEncoding" value="UTF-8"/> 26 </bean> 27 </property> 28 </bean> 29 </property> 30 </bean> 31 32 33 </beans>
8.建立TestController類:
寫一個index方法,用來訪問首頁
1 @RequestMapping("/") 2 public String index(){ 3 return "index"; 4 5 }
建立RequestMappingController類:
寫一個success方法,用來訪問success頁面,同時在類上也加一個@RequestMapping("/hello")
1 @RequestMapping("/testRequestMapping") 2 public String success(){ 3 return "success"; 4 }
上述兩個類的完整程式碼:
TestController類:
1 package com.st.springmvc.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 /** 7 * @Author: 尚廷 8 * @Date: 2021/12/21/ 14:07 9 * @Description: 10 */ 11 @Controller 12 public class TestController { 13 14 @RequestMapping("/") 15 public String index(){ 16 return "index"; 17 18 } 19 20 }
RequestMappingController類:
1 package com.st.springmvc.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 /** 7 * @Author: 尚廷 8 * @Date: 2021/12/21/ 14:26 9 * @Description: 10 */ 11 @Controller 12 @RequestMapping("/hello") 13 public class RequestMappingController { 14 @RequestMapping("/testRequestMapping") 15 public String success(){ 16 return "success"; 17 } 18 19 }
*在類上加@RequestMapping相當於有多了一層路徑。大薩
因為@RequestMapping的對映請求是惟一的,所以當專案業務模組較多時,
名字有可能重複,在類上加@RequestMapping,模組重名時不需要修改,而且便於區分
9.在webapp\WEB-INF\templates下建立index.html和success.html
index.html裡面內容:<a th:href="@{/hello/testRequestMapping}">點選測試success頁面</a>
success.html內容:<h1>這裡是success頁面!</h1>
如果不加之前在類上的對映hello將會報錯,因為路徑是不正確的。
*Thymeleaf自動新增上下文路徑,所以不需要寫。
10.啟動Tomcat,點選首頁中的超連結,訪問成功!
總結:
1.@RequestMapping註解的功能
單詞意思見名知意即請求對映,@RequestMapping作用就是將請求與處理請求的控制器方法關聯起來
建立對映關係。
2.@RequestMapping註解的位置
@RequestMapping可以標識類和方法:
(1).當標識一個類時:設定對映請求的請求路徑的初始資訊
(2).@RequestMapping標識一個方法:設定對映請求請求路徑的具體資訊
所以在路徑上應該加上類上所設定的RequestMapping的value,
即:多加一個hello (類--->方法)
<a th:href="@{/hello/testRequestMapping}">點選測試success頁面</a>