如何通过修改配置文件处理静态资源的请求
在项目开发过程中,我们需要把一些静态资源文件存放到服务端,对于客户端的请求我们直接进行处理即可,而不用再到控制层中再进行处理。
1、首先我们了解Tomcat下的路径
在Tomcat服务器中,我们的根路径是在webapp下,即webapp是我们的根目录,因此我们响应相应的静态资源只需要以该目录为根目录进行分析即可。
2、处理静态资源
在spring框架中,我们通过编写配置文件来编辑我们的servlet。在相应的servlet中我们可以编写关于返回静态资源的部分,我们在scr/main/resource中创建service-servlet.xml文件,并且添加如下配置:<mvc:resources location="/" mapping="/getHtml/**"/>。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd ">
<!-- 开启注解 -->
<!-- spring MVC 的一键式配置方法 -->
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.mediAssis">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/html/" />
<property name="suffix" value=".html" />
</bean>
<!-- =======================图片上传=========================== -->
<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<!-- 指定所上传文件的总大小,单位字节。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="10240000" />
</bean>
<!-- =======================静态资源访问=========================== -->
<mvc:resources location="/" mapping="/getHtml/**"/>
</beans>