15

我已经“暂时”创建了一个简单而基本的 Spring Web 应用程序。我习惯于将部署描述符作为简单的 web.xml 文件,然后将应用程序上下文作为 xml 文件。

不过,现在我想尝试只使用 java 文件来创建我的整个 Spring Web 应用程序。因此,我创建了 WebApplicationInitializer 而不是正常的部署描述符,以及使用 @Configuration 注释的应用程序上下文。

部署描述符

package dk.chakula.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
/**
 *
 * @author martin
 * @since 12-1-2012
 * @version 1.0
 */
public class Initializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {
        registerDispatcherServlet(servletContext);
    }

    private void registerDispatcherServlet(final ServletContext servletContext) {
        WebApplicationContext dispatcherContext = createContext(ChakulaWebConfigurationContext.class);
        DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);
        Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

    private WebApplicationContext createContext(final Class<?>... annotatedClasses) {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(annotatedClasses);
        return context;
    }

} //End of class Initializer

应用程序上下文

package dk.chakula.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.springframework.web.servlet.view.tiles2.TilesView;

/**
 *
 * @author martin
 * @since 12-01-2013
 * @version 1.0
 */
@Configuration
@EnableWebMvc
@ComponentScan("dk.chakula.web")
public class ChakulaWebConfigurationContext {

    @Bean
    public TilesConfigurer setupTilesConfigurer() {
        TilesConfigurer configurer = new TilesConfigurer();
        String[] definitions = {"/layout/layout.xml"};
        configurer.setDefinitions(definitions);
        return configurer;
    }

    @Bean
    public UrlBasedViewResolver setupTilesViewResolver() {
        UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
        viewResolver.setViewClass(TilesView.class);
        return viewResolver;
    }

} //End of class ChakulaWebConfigurationContext

我的问题是我似乎无法找到一种方法“隔离”我到包含图像、css javascript 等的资源文件夹的映射。当我的应用程序上下文在 java 中时。

对于普通的 XML 应用程序上下文,我使用此标记来隔离到 /resources/ 的映射

<mvc:resources mapping="/resources/**" location="/resources/" />

我该怎么做,这样我的 Web 应用程序才能使用我的图像、css 等。

4

3 回答 3

33

为了能够在 Spring MVC 应用程序中提供静态资源,您需要两个 XML 标记:<mvc:resources/><mvc:default-servlet-handler/>. 在基于 Java 的 Spring 配置中相同的是:

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    // equivalents for <mvc:resources/> tags
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(31556926);
        registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(31556926);
        registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(31556926);
    }

    // equivalent for <mvc:default-servlet-handler/> tag
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    // ... other stuff ...
}

请注意,由于@EnableWebMvc使用了注释,因此无需直接扩展WebMvcConfigurationSupport,您只需扩展即可WebMvcConfigurerAdapter。有关详细信息,请参阅@EnableWebMvc的 JavaDoc。

于 2013-06-09T19:40:23.940 回答
9

在互联网上使用数小时搜索仅使用 java 文件阅读有关 Spring MVC 3 的文章后,我发现了一些文章,这些文章使用了从 WebMvcConfigurationSupport 类扩展的方法,然后覆盖了 2 个方法 - addResourceHandler(ResourceHandlerRegistry) 和 ResourceHandlerMapping()。

我的新应用程序上下文现在看起来像这样。

package dk.chakula.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.springframework.web.servlet.view.tiles2.TilesView;

/**
 *
 * @author martin
 * @since 12-01-2013
 * @version 1.0
 */
@Configuration
@EnableWebMvc
@ComponentScan("dk.chakula.web")
public class ChakulaWebConfigurationContext extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Override
    @Bean
    public HandlerMapping resourceHandlerMapping() {
        AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) super.resourceHandlerMapping();
        handlerMapping.setOrder(-1);
        return handlerMapping;
    }

    @Bean
    public TilesConfigurer setupTilesConfigurer() {
        TilesConfigurer configurer = new TilesConfigurer();
        String[] definitions = {"/layout/layout.xml"};
        configurer.setDefinitions(definitions);
        return configurer;
    }

    @Bean
    public UrlBasedViewResolver setupTilesViewResolver() {
        UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
        viewResolver.setViewClass(TilesView.class);
        return viewResolver;
    }

} //End of class ChakulaWebConfigurationContext

据我了解,我们必须重写 addResourceHandler,将资源的位置和映射添加到注册表。此后,我们需要一个返回 HandlerMapping 对象的 bean。这个 HandlerMapping 的顺序应该设置为 -1,因为我可以从 spring 文档中读到,然后 -1 表示

HandlerMapping 以 Integer.MAX_VALUE-1 排序以服务静态资源请求。

我的应用程序现在可以将 css 文件和图像加载到他们的视图中,我想用答案来启发其他人,以便将来的人们可以从中受益。

于 2013-01-13T08:03:05.293 回答
4

试试这个:

@Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
于 2015-06-22T06:01:29.847 回答