Spring MVC注解式開發(fā)示例完整過程
項目案例
用 RequestMapping 注解式開發(fā)開發(fā)設置一個項目,實現(xiàn)在瀏覽器中輸入 http://localhost:8080/springmvc02/first/show,輸出網(wǎng)頁內(nèi)容 “我的第一個注解式 Spring MVC 開發(fā)程序!”。
實現(xiàn)步驟
項目創(chuàng)建添加依賴
在 IDea 中新建一個項目 springmvc02,創(chuàng)建如下圖所示的目錄結(jié)構(gòu):
項目創(chuàng)建好之后,打開 pom.xml 文件,添加依賴內(nèi)容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.kgc.springmvc02</groupId> <artifactId>springmvc02</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>springmvc02 Maven Webapp</name> <url>http://maven.apache.org</url> <!--第1步:添加需要的 JAR 包--> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.19</version> </dependency> </dependencies> <build> <finalName>springmvc02</finalName> </build> </project>
配置web.xml文件
在 web.xml 文件里配置 DispatcherServlet 前端控制器,項目 webapp/WEB-INF 目錄里的 web.xml 文件配置如下:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--第2步:配置前端控制器--> <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:spring-config.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
客戶端發(fā)出的 URL 請求都會被 DispatcherServlet(前端控制器)攔截 ,DispatcherServlet 再交給 spring-config.xml 進行處理。
配置spring-config.xml文件
配置 handlerMapping 處理器映射器。
在 src/main/resources 目錄下新建一個 xml 文件,命名為 spring-config.xml,輸入如下關鍵內(nèi)容:
<?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" 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"> <!--配置處理器映射器--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> </beans>
上面代碼意思是創(chuàng)建一種類型為 RequestMappingHandlerMapping 的處理器映射器,即定義一種 “請求/響應” 映射規(guī)則,客戶端的 Url 請求如果跟某一個 bean 的 name 屬性匹配,則由該 bean 的 class 屬性指定的控制器 Controller 類進行響應處理。
配置 HandlerAdapter 處理器適配器。
配置完處理器映射器后,接著在 spring-config.xml 中插入如下內(nèi)容(插入位置在處理器映射器下方,節(jié)點 </beans> 的上方):
<!--配置處理器適配器--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
該代碼的意思是創(chuàng)建一種處理器適配器,類型為 RequestMappingHandlerAdapter,用于對上述指定的控制器 Controller 類的 handleRequest() 方法的調(diào)用與執(zhí)行。
配置 視圖解析器。
視圖解釋器 用來解釋控制器返回的邏輯視圖的真實路徑,這樣更方便,易于擴展。在 spring-config.xml 中輸入代碼:
<!--配置視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--前綴配置--> <property name="prefix" value="/"></property> <!--后綴配置--> <property name="suffix" value=".jsp"></property> </bean>
上面代碼的意思是控制器 Controller 返回的邏輯視圖,需要加上 前綴 “/” 和 后綴 “.jsp”,最后拼接成完整的視圖路徑。比如本例中,Controller 返回的視圖為 “show”,視圖解釋器將為它加上前綴后綴,最終構(gòu)成完整路徑為 “/ show.jsp”。視圖解釋器不是非要不可,如果沒有視圖解釋器,則 Controller 返回的視圖必須打上完整路徑的視圖名稱。
配置 組件掃描器
<!--開啟包掃描 base-package 設置需要掃描的包 --> <context:component-scan base-package="cn.kgc.springmvc02"></context:component-scan>
編寫一個Controller類
在 cn.kgc.springmvc02.controller 下新建一個類 TestController,代碼如下:
package cn.kgc.springmvc02.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("first") public class TestController { @RequestMapping("show") private String show(){ return "show"; } }
第一個注解 @Controller 表示將本類定義為一個控制器類,這個類無須再實現(xiàn) Controller 接口。
第二個注解 @RequestMapping(“first”) 表示定義一種 “請求/響應” 的映射關系,即如果客戶端瀏覽器發(fā)出 “first” 的 url 請求則由該注解下面的 show() 方法來響應,即瀏覽器通過 url 路徑+“first/show” 就可訪問到本方法,url 請求能夠直接映射到控制器類的方法級別。這樣一個簡單的注解,就輕松的取代了之前的處理器映射器和 bean 的配置,大大減少了配置工作量。
創(chuàng)建響應頁面
在 webapp 目錄下創(chuàng)建文件 show.jsp 頁面,內(nèi)容如下:
<%-- Created by IntelliJ IDEA. To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>我的第一個注解式 Spring MVC 開發(fā)程序!</h1> </body> </html>
運行并測試程序
啟動運行 Tomcat,打開瀏覽器后,運行 “http://localhost:8080/springmvc02/first/show”,運行效果如下:
到此這篇關于Spring MVC注解式開發(fā)示例完整過程的文章就介紹到這了,更多相關Spring MVC注解式開發(fā)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于Java和GeoTools的Shapefile矢量數(shù)據(jù)縮略圖生成實踐
這篇文章主要介紹了基于Java和GeoTools的Shapefile矢量數(shù)據(jù)縮略圖生成實踐,需要的朋友可以參考下2024-08-08解決分頁插件pagehelper在SpringBoot不起作用的問題
這篇文章主要介紹了解決分頁插件pagehelper在SpringBoot不起作用的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05Java JVM運行時數(shù)據(jù)區(qū)(Run-Time Data Areas)
運行時數(shù)據(jù)區(qū),是java虛擬機定義的在程序執(zhí)行期間使用的各種運行時的數(shù)據(jù)區(qū),通過JVM運行時數(shù)據(jù)區(qū)圖例給大家展示的很詳細,對JVM 運行時數(shù)據(jù)區(qū)相關知識感興趣的朋友跟隨小編一起看看吧2021-06-06HttpClient的DnsResolver自定義DNS解析另一種選擇深入研究
這篇文章主要為大家介紹了HttpClient的DnsResolver自定義DNS解析另一種選擇深入研究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10