6

如何在 spring 中设置处理程序映射的优先级以允许资源处理程序在控制器请求映射之前映射?

例如这个配置:

@Configuration
@EnableWebMvc
@ComponentScan("org.commons.sandbox")
public class WebConfiguration extends WebMvcConfigurerAdapter {

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

    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

使用以下控制器:

@Controller
public class HomeController {

    @RequestMapping("/**")
    public String home(){
        return "home";
    }
}

捕获对“/resources”的请求。所以链接到css文件是jsp返回“home”视图而不是“resources”目录中的实际css文件。我知道这是由于映射“/ **”,但我认为有一种方法可以配置映射顺序......有吗?

谢谢!

4

2 回答 2

6

好吧,这是我的解决方案......现在。

我必须WebMvcConfigurationSupport直接扩展而不是使用@EnableWebMvc

这是我更新的配置

@Configuration
@ComponentScan("org.commons.sandbox")
public class WebConfiguration extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        //just to be clear that this has a priority of 0, this is default anyway
        registry.setOrder(0);
    }

    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Override
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping handler = super.requestMappingHandlerMapping();
            //now i have a handle on the handler i can lower it's priority
            //in the super class implementation this is set to 0 
        handler.setOrder(1);
        return handler;
    }
}

我现在可以将优先级设置RequestMappingHandlerMapping为低于SimpleUrlHandlerMapping资源处理程序,以便它在控制器类有机会之前解决静态资源请求

于 2013-06-29T23:33:03.420 回答
1

就我而言,我SimpleUrlHandlerMapping之前需要ResourceHandler. 解决了setOrder()。春季启动代码:

@EnableAutoConfiguration
@Configuration
@Slf4j
public class MvcConfiguration extends WebMvcConfigurerAdapter {

    @Bean(name = "urlViewController")
    public UrlFilenameViewController getUrlViewController() {
        return new UrlFilenameViewController();
    }

    @Bean
    public SimpleUrlHandlerMapping getUrlHandlerMapping() {
        SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
        handlerMapping.setMappings(new Properties() {
            {
                put("/view/*", "urlViewController");
            }
        });
        handlerMapping.setOrder(Integer.MAX_VALUE - 5);
        return handlerMapping;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("file:./resources/static/");
        ....
        registry.setOrder(Integer.MAX_VALUE);
    }
}
于 2017-06-22T22:14:02.003 回答