03springmvc项目使用静态资源

静态资源
图片
样式
脚本
静态网页(.html)


控制器
XxxxController.java

/WEB-INF/template/show.jsp 模板(视图层显示)


如果springmvc框架项目配置如下:
<!-- springmvc 4.3.9 -->
<servlet>
<servlet-name>smvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 配置让springmvc 配置文件位置 classes目录就myeclipse src目录 maven resources目录 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:smvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>smvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

src/main/resources/smvc.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.fz.controller" />

<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<mvc:annotation-driven/>
<!-- 视图显示静态资源 -->
<mvc:resources mapping="/imgs/**" location="/imgs/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
</beans>


补充一下:
<!-- 在没有配置mvc:resources的时候没有问题,一旦配置了mvc:resources,注解方式的url就没有加载 -->
<mvc:annotation-driven/>
<mvc:resources mapping="/imgs/**" location="/imgs/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>

如果如下配置
<!-- springmvc 4.3.9 -->
<servlet>
<servlet-name>smvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 配置让springmvc 配置文件位置 classes目录就myeclipse src目录 maven resources目录 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:smvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>smvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

静态资源直接可以访问,所有控制器扩展名为.action

怕什么真理无穷,进一步有一步的欢喜
原文地址:https://www.cnblogs.com/Mkady/p/7200821.html