How to use spring MVC's tag in a java application context?

后端 未结 3 910
误落风尘
误落风尘 2020-12-05 03:35

I have created \'for now\' a simple and basic spring web application. I am used to have a deployment descriptor as a simple web.xml file, and then an application context as

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 03:54

    After using hours searching on the internet reading about Spring MVC 3 using only java files I fell over some articles which used an approach by extending from WebMvcConfigurationSupport class, and then overriding 2 methods - addResourceHandler( ResourceHandlerRegistry ) and ResourceHandlerMapping().

    My new Application context now look like this.

    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
    

    As I understood We had to override addResourceHandler, to add the location and the mapping of resources to the registry. Thereafter we needed a bean which returned an object of HandlerMapping. The order of this HandlerMapping should be set to -1, because as I could read from the spring documentation, then -1 means

    HandlerMapping ordered at Integer.MAX_VALUE-1 to serve static resource requests.

    My application can now load the css files and images into their views, and I wanted to enlighten you others with the answer so, people in the future could get benefit of this.

提交回复
热议问题