9、SpringMVC之处理静态资源

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

posted @ 2023-10-26 21:18  Javaer1995  阅读(149)  评论(0编辑  收藏  举报