如何在Spring应用程序中正确配置静态资源处理程序?

时间:2015-08-21 13:02:48

标签: java spring spring-mvc java-ee resources

我在Spring很新,我有以下问题。

在教程中我找到了这个配置类示例:

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = { "com.mycompany.myproject.web.controller" })
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
.................
..............

}

以前的代码段中 addResourceHandlers()方法究竟如何运作?查看官方文档,它似乎理解它添加处理程序以提供静态资源,例如来自Web应用程序根目录下的特定位置的图像,js和css文件,类路径等。

在前一种情况下,它用于添加与Twitter BootStrap框架相关的静态资源(类似于CSS文件和JavaScript文件)。

问题是我正在工作的项目不使用Java配置但是使用 XML配置而且我有一些问题需要了解我怎样才能做同样的事情。代码片段到我的XML配置中。

特别是我有一个包含MVC配置的 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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.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/" />

    <!-- 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="it.hp.miur" />

</beans:beans>

我认为我必须将此配置放入此文件中,但我不知道该怎么做。你能救我吗?

TNX

0 个答案:

没有答案
相关问题