首页 > 其他分享 >9、SpringMVC之处理静态资源

9、SpringMVC之处理静态资源

时间:2023-10-26 21:37:14浏览次数:28  
标签:web 处理 SpringMVC springframework 静态 module org 9.1

9.1、环境搭建

9.1.1、在project创建新module

image

9.1.2、选择maven

image

9.1.3、设置module名称和路径

image

image

9.1.4、module初始状态

image

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

image

注意:默认的打包方式为 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的实现) -->
        <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资源目录

image

打开Project Structure,选择对应的module,并为该module创建一个web.xml文件

image

注意:web.xml文件需要放到web资源路径(工程路径\src\main\webapp)下

image

9.1.7、配置web.xml

image

<?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,对浏览器发送的请求统一进行处理-->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--通过初始化参数指定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>
            <!--该初始化参数用于设置请求参数的编码方式-->
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <!--该初始化参数用于设置响应参数也使用同样的编码方式-->
            <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、创建SpringMVC的配置文件

image

<?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):为指定的请求直接设置(逻辑)视图名称,从而实现页面的跳转
    -->
    <mvc:view-controller path="/" view-name="index"></mvc:view-controller>

</beans>

9.1.9、创建请求控制器

image

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、创建模板目录及页面模板

image

注意: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>

image

<!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

image

image

9.2、引用图片的失败示例

9.2.1、创建图片目录并放置图片

image

9.2.2、在html引用工程中的图片

image

<img th:src="@{/imgs/JAVAEE.png}">

9.2.3、测试效果

image

image

9.3、页面跳转的失败示例

9.3.1、页面请求示例

image

<a th:href="@{/static/page}">测试通过请求转发直接跳转到一个页面</a>

9.3.2、控制器方法示例

image

    @RequestMapping("/static/page")
    public String toStaticPage(){
        return "forward:/WEB-INF/templates/success.html";
    }

9.3.3、测试效果

image

image

image

9.4、失败原因分析

image

因为DispatcherServlet接管了所有请求,所以导致tomcat中处理静态资源的默认Servlet不再生效。

9.5、解决方案

image

开启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、引用图片

image

9.6.2、页面跳转

image

image

image

标签:web,处理,SpringMVC,springframework,静态,module,org,9.1
From: https://www.cnblogs.com/Javaer1995/p/17777472.html

相关文章

  • 优雅的使用String字符串处理各种类型转换
    (文章目录)......
  • vuex 的数据丢失如何处理?
    方法一:存储在LocalStorage、SessionStorage、IndexDB等。这些都是浏览器的API,可以将数据存储在硬盘上,做持久化存储。在初始化state数据的时候,从localStorage中获取:state={userInfo:localStorage.getItem('userInfo')}由于localStorage不是响应式的,需要手......
  • 微信公众号-XML数据接收与处理
    第一步:接收微信发来的xml数据有以下两种方式$xml=$GLOBALS['HTTP_RAM_POST_DATA'];//php7版本以上不能使用$xml=file_get_contents('php://input');第二步:对接收过来的数据进行处理libxml_disable_entity_loader(true);$obj=simplexml_load_string($postStr,'Simp......
  • Android图片进行高斯模糊处理/毛玻璃效果
    android中实现毛玻璃效果的方法比较多,有用java实现图片处理算法的,也有把算法用c/c++实现并用jni调用的,而实现毛玻璃的开源库在github上也有不少.其实google的官方sdk中也为我们提供了这样的工具,本着能用官方尽量不自己实现,能自己实现尽量不用第三方的原则,官方的实现方......
  • iOS自动混淆测试处理笔记
    ​ 1 打开ipa,导出ipa 路径和配置文件路径会自动填充   ​2 点击开始自动混淆测试处理自动混淆测试是针对oc 类和oc方法这两个模块进行自动混淆ipa,并ipa安装到设备中运行,通过检测运行ipa包是否崩溃,来对oc类和oc方法进行筛选。如果崩溃,则该类名或方法名不可混淆......
  • vue处理文件流实现上传下载
    1.文件流转base64axios({method:"post",url:"************",responseType:"blob",//必须将返回数据格式更改为blob格式}).then(res=>{//处理返回的文件流数据转为blob对象letblob=......
  • Stirling-PDF 开源在线PDF处理系统(可解密PDF)
    Stirling-PDF是一个本地托管的Web应用程序,允许您对PDF文件执行各种操作命令行安装#运行容器dockerrun-d\--restartunless-stopped\--namespdf\-p8077:8080\frooodle/s-pdf运行在浏览器中输入http://IP:8077就能看到主界面......
  • H3C G3服务器硬盘报错后立即自动rebuilding处理
    2023-10-0722:16:40RAID_Array触发 严重 TransitiontoCriticalfromlesssevere---PCIeslot:22023-10-0722:19:36 HDD_F02_Status DriveSlot(Bay) 触发 正常 Rebuild/Remapinprogress2023-10-0901:20:59 HDD_F02_Status DriveSlot(Bay) 解除 正常 Rebuild/Remapin......
  • 《最新出炉》系列初窥篇-Python+Playwright自动化测试-23-处理select下拉框-下篇
    1.简介上一篇中宏哥主要讲解和分享了一下,我们常见或者传统的select下拉框的操作,但是近几年又出现了了一种新的select下拉框,其和我们传统的select下拉框完全不一样,那么我们如何使用playwright对其进行定位操作了。宏哥今天就来讲解和分享一下仅供大家参考,不喜勿喷。2.新的select......
  • Linux (KDE) 中使用Network Settings设置静态ip
    在Linux(KDE)中使用NetworkSettings设置s5静态IP详细教程。首先,打开KDE的设置面板。可以通过点击桌面上的设置图标,或者在开始菜单中搜索“Settings”并打开。在设置面板中,点击“Network”选项。接下来,你会看到一个“NetworkConnections”的窗口。在这个窗口中,你需......