使用注解開發(fā)SpringMVC詳細配置教程
1、使用注解開發(fā)SpringMVC
1、新建一個普通的maven項目,添加web支持
2、在pom.xml中導(dǎo)入相關(guān)依賴
SpringMVC相關(guān)
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.8.RELEASE</version> </dependency>
Servlet
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency>
jsp
<dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2 </version> </dependency>
為了防止資源導(dǎo)出失敗,我們加入以下代碼
<!--在build中配置resources,防止我們資源導(dǎo)出失敗的問題--> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
3、配置web.xml
注意web.xml的版本要為最新版
注冊DispatcherServlet
- 需要綁定一個SpringMVC配置文件,下一步我們將創(chuàng)建
- 設(shè)置啟動級別為1
- 設(shè)置映射路徑為
/
<!--1.注冊DispatcherServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--關(guān)聯(lián)一個springmvc的配置文件:【servlet-name】-servlet.xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!--啟動級別-1--> <load-on-startup>1</load-on-startup> </servlet> <!--/ 匹配所有的請求;(不包括.jsp)--> <!--/* 匹配所有的請求;(包括.jsp)--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
4、編寫SpringMVC配置文件
上述
DispatcherServlet
綁定該配置文件,主要配置以下幾個部分:
1. 自動掃描包
讓指定包下的注解生效,由IOC容器統(tǒng)一管理
<context:component-scan base-package="controller"/>
2. 過濾靜態(tài)資源
它會像一個檢查員,對進入DispatcherServlet
的URL進行篩查,如果發(fā)現(xiàn)是靜態(tài)資源的請求,就將該請求轉(zhuǎn)由Web應(yīng)用服務(wù)器默認的Servlet處理,如果不是靜態(tài)資源的請求,才由DispatcherServlet
繼續(xù)處理。
<mvc:default-servlet-handler/>
3. 支持mvc注解驅(qū)動
在Spring中一般用
@RequestMapping
注解來完成映射關(guān)系
為了使其生效, 必須向上下文中注冊兩個實例:
- DefaultAnnotationHandLerMapping(處理器映射器)
- AnnotationMethodHandLerAdapter(處理器適配器)
<mvc:annotation-driven/>
annotation-driven配置幫助我們自動完成上述兩個實例的注入
4. 視圖解析器
<!--視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver"> <!--前綴--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--后綴--> <property name="suffix" value=".jsp"/> </bean>
完整代碼:
<?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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--自動掃描包,讓指定包下的注解生效,由IOC容器統(tǒng)一管理--> <context:component-scan base-package="controller"/> <!--過濾靜態(tài)資源,讓SpringMVC不處理靜態(tài)資源 .css .js .mp3 .mp4 .html--> <mvc:default-servlet-handler/> <!-- 支持mvc注解驅(qū)動 在spring中一般用@RequestMapping注解完成映射關(guān)系 要想使@RequestMapping注解生效 必須向上下文中注冊DefaultAnnotationHandLerMapping(處理器映射器) 和一個AnnotationMethodHandLerAdapter(處理器適配器)實例 這兩個實例分別在類級別和方法級別處理 annotation-driven配置幫助我們自動完成上述兩個實例的注入 --> <mvc:annotation-driven/> <!--視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver"> <!--前綴--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--后綴--> <property name="suffix" value=".jsp"/> </bean> </beans>
SpringMVC必須配置的三大件
- 處理器映射器
- 處理器適配器
- 視圖解析器
當(dāng)我們用注解實現(xiàn)時,只需要手動配置視圖解析器,另外兩個只需要開啟注解驅(qū)動即可,省去了大量xml片段
5、創(chuàng)建controller
在src/main/java目錄下新建controller包,在其中新建HelloController.java
package controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/hello") public String hello(Model model) { //封裝數(shù)據(jù) model.addAttribute("msg", "Hello SpringMVCAnnotation"); return "hello";//會被視圖解析器處理 } }
- @Controller是為了讓SpringIOC容器初始化時自動掃描到該類
- @RequestMapping是為了映射請求路徑,直接再其中設(shè)置路徑名即可,這里為\hello
可以在類上使用,也可以直接在方法上使用,同時使用時,在類上使用相當(dāng)于父路徑
- 方法聲明中的Model參數(shù)是用于向視圖中封裝數(shù)據(jù)
- 方法返回的結(jié)果是視圖的名稱,這里為hello,加上配置文件中的前后綴變成WEB-INF/jsp/hello.jsp
6、創(chuàng)建視圖層
編寫要請求的jsp頁面,這里顯示上述存入視圖的參數(shù)
在web/WEB-INF/下新建jsp包,在其中新建hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} </body> </html>
7、在項目結(jié)構(gòu)中添加lib目錄
該步驟是為了防止最終出現(xiàn)404錯誤,這是IDEA自己的問題
選中所有的包,導(dǎo)入
8、配置Tomcat運行測試
運行測試,訪問http://localhost:8080/hello,成功訪問
2、控制器Controller
- 控制器復(fù)雜提供訪問應(yīng)用程序的形為,通常通過接口定義或者注解定義兩種方式實現(xiàn)
- 控制器負責(zé)解析用戶請求并將其轉(zhuǎn)換為一個模型
- 在SpringMVC中一個控制器類可以包含多個方法
- 在SpringMVC中,對于Controller的配置方式有很多種
1. 實現(xiàn)Controller接口
Controller是一個接口,在org.springframework.web.servlet.mvc包下,接口中只有一個方法;
接下來的操作代碼基于上一篇博客第一個SpringMVC程序
我們刪除springmvc-servlet配置文件中處理映射器和處理適配器的配置,只留下一個視圖解析器
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver"> <!--前綴--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--后綴--> <property name="suffix" value=".jsp"/> </bean> <!--Handler--> <bean id="/hello" class="controller.HelloController"/> </beans>
然后配置Tomcat運行測試,同樣訪問http://localhost:8080/hello
發(fā)現(xiàn)也可以成功運行,我們先前之所以寫上處理映射器和處理適配器的配置,是為了了解其執(zhí)行原理,顯示調(diào)用,真實開發(fā)中,不需要配置,SpringMVC已經(jīng)幫我們配置好了的
缺點:
一個控制器中只有一個方法,如果要多個方法則需要定義多個Controller,比較麻煩;2. 使用注解@Controller
@Controller注解類型用于聲明Spring類的實例是一個控制器,這是我們最長使用的方式
Spring可以使用掃描機制來找到應(yīng)用程序中所有基于注解的控制器類,為了保證Spring能找到你的控制器,需要在配置文件中聲明組件掃描。
<context:component-scan base-package="controller"/>
例如上述類:
@Controller public class HelloController { @RequestMapping("/hello") public String hello(Model model) { //封裝數(shù)據(jù) model.addAttribute("msg", "Hello SpringMVCAnnotation"); return "hello";//會被視圖解析器處理 } }
被這個注解的類中的所有方法,如果返間值是String,并且有具體頁面可以跳轉(zhuǎn),那么就會被視圖解析器解折
例如我們在其中增加一個方法,同樣返回hello視圖
@Controller public class HelloController { @RequestMapping("/hello") public String hello(Model model) { //封裝數(shù)據(jù) model.addAttribute("msg", "Hello SpringMVCAnnotation"); return "hello";//會被視圖解析器處理 } @RequestMapping("/hello2") public String hello2(Model model) { model.addAttribute("msg","This is the second request"); return "hello"; } }
再次配置Tomcat運行測試,首先訪問http://localhost:8080/hello
再訪問http://localhost:8080/hello2
可以發(fā)現(xiàn),我們的兩個請求都可以指向一個視圖,但是頁面結(jié)果的結(jié)果是不一樣的,從這里可以看出視圖是被復(fù)用的,而控制器與視圖之間是弱偶合關(guān)系。
3、@RequestMapping
@RequestMapping注解用于映射url到控制器或一個特定的處理程序方法,可用于類或方法上
用于類上,表示類中的所有響應(yīng)請求的方法都是以該地址作為父路徑
我們修改上述方法,在類上增加該注解
@Controller @RequestMapping("/h") public class HelloController { @RequestMapping("/hello") public String hello(Model model) { //封裝數(shù)據(jù) model.addAttribute("msg", "Hello SpringMVCAnnotation"); return "hello";//會被視圖解析器處理 } @RequestMapping("/hello2") public String hello2(Model model) { model.addAttribute("msg","This is the second request"); return "hello"; } }
然后配置Tomcat運行測試,我們再輸入http://localhost:8080/hello,
直接404找不到報錯了,這是因為我們在類上添加該注解,相當(dāng)于一個父路徑
再次訪問http://localhost:8080/h/hello,成功!
總結(jié)
到此這篇關(guān)于使用注解開發(fā)SpringMVC詳細配置教程的文章就介紹到這了,更多相關(guān)注解開發(fā)SpringMVC內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SSH框架網(wǎng)上商城項目第6戰(zhàn)之基于DataGrid的數(shù)據(jù)顯示
SSH框架網(wǎng)上商城項目第6戰(zhàn)之基于DataGrid的數(shù)據(jù)顯示,提供了豐富的選擇、排序、分組和編輯數(shù)據(jù)的功能支持,感興趣的小伙伴們可以參考一下2016-05-05jsp頁面中獲取servlet請求中的參數(shù)的辦法詳解
在JAVA WEB應(yīng)用中,如何獲取servlet請求中的參數(shù),本文講解了jsp頁面中獲取servlet請求中的參數(shù)的辦法2018-03-03spring boot加入攔截器Interceptor過程解析
這篇文章主要介紹了spring boot加入攔截器Interceptor過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10基于SpringBoot后端導(dǎo)出Excel文件的操作方法
這篇文章給大家介紹了基于SpringBoot后端導(dǎo)出Excel文件的操作方法,文中通過代碼示例給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02