1. 程式人生 > >springmvc中判斷當前訪問是否靜態資源

springmvc中判斷當前訪問是否靜態資源

在springmvc中<mvc:resources mapping="/static/**" location="/WEB-INF/static/"/> 定義了靜態資源訪問。

有時我們需要在程式中判斷當前訪問的是否是靜態資源。下面我們來實現一個通用的方案。

一。首先在spring全域性配置中定義

<bean id="resourceUrlProvider" class="org.springframework.web.servlet.resource.ResourceUrlProvider"/>

二。在springmvc的配置檔案中註冊攔截器
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor">
                <constructor-arg>
                    <ref bean="resourceUrlProvider"/>
                </constructor-arg>
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>

<mvc:resources location="xxxxx" mapping="xxxx"/>  等資訊會注入到ResourceUrlProvider裡。


三。定義一個通用獲得bean例項的類

package org.jstudioframework.spring.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.NoSuchMessageException;

import java.util.Locale;

/**
 * 在xml中配置bean,在程式中用來獲取容器中的物件例項
 */
public class GenericUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext; // Spring應用上下文環境

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        GenericUtils.applicationContext = applicationContext;
    }

    /**
     * 獲取物件
     *
     * @param name
     * @return Object 一個以所給名字註冊的bean的例項
     * @throws BeansException
     */
    public static <T> T getBean(String name) throws BeansException {
        return (T) applicationContext.getBean(name);
    }

    /**
     * 獲取型別為requiredType的物件
     *
     * @param clz
     * @return
     * @throws BeansException
     */
    public static <T> T getBean(Class<T> clz) throws BeansException {
        @SuppressWarnings("unchecked")
        T result = (T) applicationContext.getBean(clz);
        return result;
    }
}

在spring全域性配置中定義
<bean id="genericUtils"  class="org.jstudioframework.spring.utils.GenericUtils"/>

四。定義一個判斷是否是靜態訪問的工具類。
package org.jstudioframework.spring.web;

import org.jstudioframework.spring.utils.GenericUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.resource.ResourceUrlProvider;

import javax.servlet.http.HttpServletRequest;

/**
 * Servlet工具類
 */
public class Servlets {

    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static boolean isStaticFile(String uri) {
        ResourceUrlProvider resourceUrlProvider = (ResourceUrlProvider) GenericUtils.getBean(ResourceUrlProvider.class);
        String staticUri = resourceUrlProvider.getForLookupPath(uri);
        return staticUri != null;
    }
}