SpringMVC中處理靜態(tài)資源的過程詳解
9.1、環(huán)境搭建
9.1.1、在project創(chuàng)建新module
9.1.2、選擇maven
9.1.3、設置module名稱和路徑
9.1.4、module初始狀態(tài)
9.1.5、配置打包方式和引入依賴
注意:默認的打包方式為 jar,為了能配置web資源,需要將打包方式設置為 war
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ora.rain</groupId> <artifactId>spring_mvc_static</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- SpringMVC (基于依賴的傳遞性,會間接引入Spring的依賴)--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.1</version> </dependency> <!-- 日志(Thymeleaf必須要sl4j,logback則是sl4j的實現(xiàn)) --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <!-- ServletAPI --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- Spring5和Thymeleaf整合包 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.12.RELEASE</version> </dependency> </dependencies> </project>
9.1.6、配置web資源目錄
打開Project Structure,選擇對應的module,并為該module創(chuàng)建一個web.xml文件
注意:web.xml文件需要放到web資源路徑(工程路徑\src\main\webapp)下
9.1.7、配置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"> <!--配置SpringMVC的前端控制器DispatcherServlet,對瀏覽器發(fā)送的請求統(tǒng)一進行處理--> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--通過初始化參數(shù)指定SpringMVC配置文件的位置和名稱--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!--將DispatcherServlet的初始化時間提前到服務器啟動時--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--配置springMVC的編碼過濾器--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <!--該初始化參數(shù)用于設置請求參數(shù)的編碼方式--> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <!--該初始化參數(shù)用于設置響應參數(shù)也使用同樣的編碼方式--> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置處理請求方式的過濾器--> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
9.1.8、創(chuàng)建SpringMVC的配置文件
<?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"> <!--在指定的包中,掃描bean組件--> <context:component-scan base-package="online.liaojy"></context:component-scan> <!-- 配置Thymeleaf視圖解析器 --> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!-- 視圖前綴 --> <property name="prefix" value="/WEB-INF/templates/"/> <!-- 視圖后綴 --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8" /> </bean> </property> </bean> </property> </bean> <!--開啟mvc的注解驅動--> <mvc:annotation-driven></mvc:annotation-driven> <!-- 視圖控制器(mvc:view-controller):為指定的請求直接設置(邏輯)視圖名稱,從而實現(xiàn)頁面的跳轉 --> <mvc:view-controller path="/" view-name="index"></mvc:view-controller> </beans>
9.1.9、創(chuàng)建請求控制器
package online.liaojy.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author liaojy * @date 2023/10/24 - 20:15 */ @Controller public class PortalController { @RequestMapping("/") public String portal(){ return "index"; } }
9.1.10、創(chuàng)建模板目錄及頁面模板
注意:html要引入thymeleaf的約束:xmlns:th="http://www.thymeleaf.org"
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>首頁</title> </head> <body> <h1>index.html</h1> </body> </html>
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>成功</title> </head> <body> <h1>success.html</h1> </body> </html>
9.1.11、配置tomcat
9.2、引用圖片的失敗示例
9.2.1、創(chuàng)建圖片目錄并放置圖片
9.2.2、在html引用工程中的圖片
<img th:src="@{/imgs/JAVAEE.png}">
9.2.3、測試效果
9.3、頁面跳轉的失敗示例
9.3.1、頁面請求示例
<a th:href="@{/static/page}" rel="external nofollow" >測試通過請求轉發(fā)直接跳轉到一個頁面</a>
9.3.2、控制器方法示例
@RequestMapping("/static/page") public String toStaticPage(){ return "forward:/WEB-INF/templates/success.html"; }
9.3.3、測試效果
9.4、失敗原因分析
因為DispatcherServlet接管了所有請求,所以導致tomcat中處理靜態(tài)資源的默認Servlet不再生效。
9.5、解決方案
開啟Tomcat默認servlet的處理器之后,DispatcherServlet處理不了的請求(沒有對應的控制器方法)會交由Tomcat默認Servlet來處理;
注意:<mvc:default-servlet-handler>標簽要和<mvc:annotation-driven>標簽配合使用,否則所有的請求都會由Tomcat默認Servlet來處理。
<!--開啟默認servlet的處理器--> <mvc:default-servlet-handler></mvc:default-servlet-handler>
9.6、解決效果演示
9.6.1、引用圖片
9.6.2、頁面跳轉
到此這篇關于SpringMVC之處理靜態(tài)資源的文章就介紹到這了,更多相關SpringMVC靜態(tài)資源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
eclipse springboot工程打war包方法及再Tomcat中運行的方法
這篇文章主要介紹了eclipse springboot工程打war包方法及再Tomcat中運行的方法,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08教你如何將Springboot項目成功部署到linux服務器
這篇文章主要介紹了如何將Springboot項目成功部署到linux服務器上,本文分步驟給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12Java數(shù)據(jù)結構之平衡二叉樹的原理與實現(xiàn)
平衡樹(Balance Tree,BT) 指的是,任意節(jié)點的子樹的高度差都小于等于1。常見的符合平衡樹的有,B樹(多路平衡搜索樹)、AVL樹(二叉平衡搜索樹)等。本文將詳細介紹平衡二叉樹的概念和實現(xiàn)原理以及它的實現(xiàn)2022-01-01利用openoffice+jodconverter-code-3.0-bate4實現(xiàn)ppt轉圖片
這篇文章主要為大家詳細介紹了利用openoffice+jodconverter-code-3.0-bate4實現(xiàn)ppt轉圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07解讀System.getProperty("ENM_HOME")中的值從哪獲取的
這篇文章主要介紹了解讀System.getProperty("ENM_HOME")中的值從哪獲取的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12使用Java和Selenium實現(xiàn)滑塊驗證的自動化登錄功能
在現(xiàn)代Web應用中,滑塊驗證碼被廣泛用于防止自動化腳本的濫用,滑塊驗證通常要求用戶通過拖動滑塊來完成驗證,然而,在某些場景下,如自動化測試或批量登錄,我們需要通過編程手段解決滑塊驗證問題,本文將詳細介紹如何使用Java和Selenium實現(xiàn)滑塊驗證的自動化登錄2025-01-01