SpringMVC中處理靜態(tài)資源的過程詳解
9.1、環(huán)境搭建
9.1.1、在project創(chuàng)建新module

9.1.2、選擇maven

9.1.3、設(shè)置module名稱和路徑


9.1.4、module初始狀態(tài)

9.1.5、配置打包方式和引入依賴

注意:默認(rèn)的打包方式為 jar,為了能配置web資源,需要將打包方式設(shè)置為 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 (基于依賴的傳遞性,會(huì)間接引入Spring的依賴)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.1</version>
</dependency>
<!-- 日志(Thymeleaf必須要sl4j,logback則是sl4j的實(shí)現(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,選擇對(duì)應(yīng)的module,并為該module創(chuàng)建一個(gè)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,對(duì)瀏覽器發(fā)送的請(qǐng)求統(tǒng)一進(jìn)行處理-->
<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的初始化時(shí)間提前到服務(wù)器啟動(dòng)時(shí)-->
<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è)置請(qǐng)求參數(shù)的編碼方式-->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<!--該初始化參數(shù)用于設(shè)置響應(yīng)參數(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>
<!--配置處理請(qǐng)求方式的過濾器-->
<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的注解驅(qū)動(dòng)-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--
視圖控制器(mvc:view-controller):為指定的請(qǐng)求直接設(shè)置(邏輯)視圖名稱,從而實(shí)現(xiàn)頁面的跳轉(zhuǎn)
-->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
</beans>9.1.9、創(chuàng)建請(qǐ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、測(cè)試效果


9.3、頁面跳轉(zhuǎn)的失敗示例
9.3.1、頁面請(qǐng)求示例

<a th:href="@{/static/page}" rel="external nofollow" >測(cè)試通過請(qǐng)求轉(zhuǎn)發(fā)直接跳轉(zhuǎn)到一個(gè)頁面</a>9.3.2、控制器方法示例

@RequestMapping("/static/page")
public String toStaticPage(){
return "forward:/WEB-INF/templates/success.html";
}9.3.3、測(cè)試效果



9.4、失敗原因分析

因?yàn)镈ispatcherServlet接管了所有請(qǐng)求,所以導(dǎo)致tomcat中處理靜態(tài)資源的默認(rèn)Servlet不再生效。
9.5、解決方案

開啟Tomcat默認(rèn)servlet的處理器之后,DispatcherServlet處理不了的請(qǐng)求(沒有對(duì)應(yīng)的控制器方法)會(huì)交由Tomcat默認(rèn)Servlet來處理;
注意:<mvc:default-servlet-handler>標(biāo)簽要和<mvc:annotation-driven>標(biāo)簽配合使用,否則所有的請(qǐng)求都會(huì)由Tomcat默認(rèn)Servlet來處理。
<!--開啟默認(rèn)servlet的處理器-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>9.6、解決效果演示
9.6.1、引用圖片

9.6.2、頁面跳轉(zhuǎn)



到此這篇關(guān)于SpringMVC之處理靜態(tài)資源的文章就介紹到這了,更多相關(guān)SpringMVC靜態(tài)資源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java web開發(fā)之servlet圖形驗(yàn)證碼功能的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了java web開發(fā)之servlet中圖形驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Java在ElasticSearch中使用LocalDatetime類型
最近在開發(fā)一個(gè)搜索功能的需求的時(shí)候,遇到了LocalDatetime類型不能保存到ElasticSearch中的問題,這篇文章主要介紹了Java在ElasticSearch中使用LocalDatetime類型2023-10-10
使用HttpSessionListener監(jiān)聽器實(shí)戰(zhàn)
這篇文章主要介紹了使用HttpSessionListener監(jiān)聽器實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot+WebSocket實(shí)現(xiàn)即時(shí)通訊功能(J2EE方式)
WebSocket是一種在單個(gè)TCP連接上進(jìn)行全雙工通信的協(xié)議,WebSocket使得客戶端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡(jiǎn)單,允許服務(wù)端主動(dòng)向客戶端推送數(shù)據(jù),本文給大家介紹了SpringBoot+WebSocket實(shí)現(xiàn)即時(shí)通訊功能(J2EE方式),需要的朋友可以參考下2025-01-01
Spring Boot spring-boot-maven-plugin 參數(shù)配置詳解(最
文章介紹了Spring Boot Maven插件的5個(gè)核心目標(biāo)(repackage、run、start、stop、build-info)及其應(yīng)用場(chǎng)景,涵蓋應(yīng)用打包、運(yùn)行、集成測(cè)試、構(gòu)建信息生成、依賴管理等,同時(shí)說明了如何配置pom.xml、調(diào)試參數(shù)及隨機(jī)端口設(shè)置,感興趣的朋友一起看看吧2025-07-07
輕松學(xué)會(huì)使用JavaMail?API發(fā)送郵件
想要輕松學(xué)會(huì)使用JavaMail?API發(fā)送郵件嗎?本指南將帶你快速掌握這一技能,讓你能夠輕松發(fā)送電子郵件,無論是個(gè)人還是工作需求,跟著我們的步驟,很快你就可以在Java應(yīng)用程序中自如地處理郵件通信了!2023-12-12
SpringBoot集成Swagger2構(gòu)建可視化API文檔的完整步驟
在前后端分離開發(fā)中,API文檔是連接前后端的重要橋梁,Swagger作為一款強(qiáng)大的API文檔工具,能自動(dòng)生成交互式API文檔,極大提升開發(fā)效率,本文將詳細(xì)介紹SpringBoot項(xiàng)目集成Swagger2的完整步驟,包含配置細(xì)節(jié)、常見問題及解決方案,需要的朋友可以參考下2025-07-07

