• SpringMVC访问静态资源


    在SpringMVC中常用的就是Controller与View。但是我们常常会需要访问静态资源,如html,js,css,image等。

    默认的访问的URL都会被DispatcherServlet所拦截,但是我们希望静态资源可以直接访问。该肿么办呢?

    在配置文件:web.xml可以看到:

    复制代码
    复制代码
        <!-- Processes application requests -->
        <servlet>
            <servlet-name>appServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
            
        <servlet-mapping>
            <servlet-name>appServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    复制代码
    复制代码

    静态资源访问,其实方法有多种,如:通过开放tomcat的defaultServlet,修改默认的url-parttern。

    但是SpringMVC提供了更为便捷的方式处理静态资源。

    解决方案:

    直接在servlet-context.xml中添加资源映射。

    我的开发环境:

    1、Eclipse Luna SP1

    2、Springsource-tool-suite 3.6.4 

    修改servlet-context.xml,添加resource映射即可。

    servlet-context.xml的路径如下:

    配置文件内容:

    复制代码
    复制代码
    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            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">
    
        <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
        
        <!-- Enables the Spring MVC @Controller programming model -->
        <annotation-driven />
    
        <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
        <resources mapping="/resources/**" location="/resources/" />
        <resources mapping="/images/**" location="/images/" />
        <resources mapping="/js/**" location="/js/" />
    
        <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
        <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".jsp" />
        </beans:bean>
        
        <context:component-scan base-package="com.yank.firstapp" />        
        
    </beans:beans>
    复制代码
    复制代码
  • 相关阅读:
    CareerCup Questions List 职业杯题目列表
    [CareerCup] Guards in a museum 博物馆的警卫
    [LeetCode] 7. Reverse Integer 翻转整数
    Python笔记11------一个K-means聚类的小例子
    python笔记10-----便捷网络数据NLTK语料库
    Python笔记9-----不等长列表转化成DataFrame
    Python笔记8----DataFrame(二维)
    Python笔记7----Pandas中变长字典Series
    Python笔记6----数组
    Python笔记5----集合set
  • 原文地址:https://www.cnblogs.com/zfx123--/p/7439846.html
Copyright © 2020-2023  润新知