方案1: 在web.xml中配置<servlet-name>default</servlet-name> ,用defaultServlet 先处理请求如:
- <servlet-mapping>
- <servlet-name>default</servlet-name>
- <url-pattern>*.jpg</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>default</servlet-name>
- <url-pattern>*.png</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>default</servlet-name>
- <url-pattern>*.gif</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>default</servlet-name>
- <url-pattern>*.ico</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>default</servlet-name>
- <url-pattern>*.gif</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>default</servlet-name>
- <url-pattern>*.js</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>default</servlet-name>
- <url-pattern>*.css</url-pattern>
- </servlet-mapping>
方案2:使用<mvc:resources /> 标签声明静态资源路径
- <mvc:resources mapping="/resources/js/**" location="/js/" />
- <mvc:resources mapping="/resources/images/**" location="/images/" />
- <mvc:resources mapping="/resources/css/**" location="/css/" />
方案3:使用mvc:default-servlet-handler/标签
SpringBoot解决方案:继承WebMvcConfigurerAdapter实现addResourceHandlers方法。
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/**")
- .addResourceLocations("classpath:/resource/")//sso静态资源
- .addResourceLocations("classpath:/META-INF/resources/")//swagger静态资源
- .setCachePeriod(0);//0表示不缓存
- }
(编辑:ASP站长网)
|