springmvc <mvc:resource /> 标签使用

本文介绍如何在 Spring MVC 中正确配置静态资源路径,确保 DispatcherServlet 不会拦截静态资源请求,而是将其交由 Servlet 容器处理。通过具体示例展示了如何避免因配置不当导致的静态资源加载失败问题。

<!-- 配置静态资源 -->
<mvc:resources location="/static/" mapping="/static/**"/>

 

说明:
location元素:表示webapp目录下(即服务器根目录)的static包下的所有文件;
mapping元素:表示以/static开头的所有请求路径,如/static/a 或者/static/a/b;
 
该配置的作用是:DispatcherServlet不会拦截以/static开头的所有请求路径,并当作静态资源交由Servlet处理;
 
实例:
    当我们在项目中需要引入js,css,json等资源文件时,而你在web.xml中刚好这样配置了拦截规则

 
<servlet>
    <servlet-name>controller</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
     <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring-mvc-controller.xml
        </param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>controller</servlet-name>
    <url-pattern>/</url-pattern>  //配置成'/'即出了jsp文件外其他都拦截,'/*' 即拦截所有
</servlet-mapping>

 

 然后 当在页面上引入js,css等文件时,springMvc会拦截这些文件 。然后 你的页面就好丑了。。
 项目目录:

    

在页面上这样应用就可以了

   <link rel="stylesheet" type="text/css" href="static/easyui/themes/default/easyui.css"/>
    <link rel="stylesheet" type="text/css" href="static/easyui/themes/icon.css"/>

 <script src="static/js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
 
  

 

 

 

 

转载于:https://www.cnblogs.com/gzulmc/p/6746174.html

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <context-param> <param-name>metadata-complete</param-name> <param-value>true</param-value> </context-param> <servlet> <servlet-name>exampleServlet</servlet-name> <servlet-class>com.example.ExampleServlet</servlet-class> </servlet> <servlet> <description>generated-servlet</description> <servlet-name>KuCun2 Servlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:KuCun2-web-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <description>generated-resources-servlet</description> <servlet-name>Resource Servlet</servlet-name> <servlet-class> org.springframework.js.resource.ResourceServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>exampleServlet</servlet-name> <url-pattern>/example</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Resource Servlet</servlet-name> <url-pattern>/resources/*</url-pattern> </servlet-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> </web-app>
最新发布
06-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值