美文网首页程序员
Spring MVC中如何处理静态资源

Spring MVC中如何处理静态资源

作者: 凡哥爱丽姐 | 来源:发表于2020-12-20 09:47 被阅读0次

        Spring MVC由于web.xml中配置了DispatcherServlet请求映射为“/”,将捕获Web容器所有的请求,当然也包括对静态资源的请求。Spring MVC会将它们当成一个普通请求处理,但是由于找不到对应的处理器,所以按照常规的方式引用静态文件(例如html,js,css,image等),从而使静态文件无法访问。

    1、首先我们在webapp下创建一个css文件夹用来存放样式文件(本示例中样式文件名为h1.css)

    h1{
        color: red;
    }
    

    2、index.jsp和success.jsp文件

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <link type="text/css" rel="stylesheet"  href="css/h1.css"/>
    <body>
    <h1>Hello World!</h1>
    <h2>Hello World!</h2>
    <form action="/user3" method="post">
        <input type="text" name="username"/>
        <input type="submit" value="提交">
    </form>
    </body>
    </html>
    
    
    <%--
      Created by IntelliJ IDEA.
      User: Mr Wei
      Date: 2020/5/29
      Time: 15:19
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
    <html>
    <head>
        <title>Title</title>
        <link rel="stylesheet" href="css/h1.css"/>
    </head>
    <body>
    <h1>欢迎回来</h1>
    <h2> ModelMap测试 ---> 欢迎回来${username} </h2>
    <h2> ModelAndView测试 ---> 欢迎回来${user} </h2>
    <h2> Model测试 ---> 欢迎回来${u} </h2>
    </body>
    </html>
    

    3、Controller类

    package com.fan.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class Controller3 {
    
        @RequestMapping("/user3")
        public String test(String username, Model model){
            System.out.println(username);
            model.addAttribute("u",username);
            return "success";
        }
    }
    

    4、spring.xml文件

    <?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:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           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">
        <!--扫描controller-->
        <context:component-scan base-package="com.fan.controller"></context:component-scan>
        <!--视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!--jsp所在位置-->
            <property name="prefix" value="/"></property>
            <!--jsp文件后缀名-->
            <property name="suffix" value=".jsp"></property>
        </bean>
    </beans>
    

    5、启动tomcat测试

    测试结果如下:

    测试图片.png

    从该结果我们可以看出,静态资源并没有加载出来。

    6、处理方案

    6.1、修改spring.xml配置文件

    6.1.1、添加mvc命名空间和约束

    xmlns:mvc="http://www.springframework.org/schema/mvc"
    
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    

    6.1.2、方法一(添加处理标签)

     <mvc:annotation-driven></mvc:annotation-driven><!--驱动注解-->
     <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
    

    6.1.3、方法二

     <mvc:annotation-driven></mvc:annotation-driven><!--驱动注解-->
     <mvc:default-servlet-handler></mvc:default-servlet-handler>
    

    6.2、启动tomcat测试

    测试结果如下:

    测试结果.png 测试结果.png

        这两种方法都是对spring.xml文件进行配置,都可以实现静态文件的加载。

    相关文章

      网友评论

        本文标题:Spring MVC中如何处理静态资源

        本文链接:https://www.haomeiwen.com/subject/ascjzhtx.html