Spring - 设置 HandlerMapping 优先级 [英] Spring - Set HandlerMapping Priority

查看:30
本文介绍了Spring - 设置 HandlerMapping 优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I set the priority of handler mappings in spring to allow resource handlers to map before controller request mappings?

例如这个配置:

@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 文件.我知道这是由于映射/**",但我认为有一种方法可以配置映射顺序......有吗?

Captures requests to "/resources". So linking to a css file is the jsp returns the "home" view not the actual css file in the "resources" directory. I understand that it's due to the mapping "/**" but I would assume there's a way to configure the mapping order... is there?

谢谢!

推荐答案

好吧,这是我的解决方案……暂时.

Well this is my solution... for now.

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

I have to extend from WebMvcConfigurationSupport directly instead of using @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 以便它在控制器类有机会之前解决静态资源请求

I can now set the priority of RequestMappingHandlerMapping to be below SimpleUrlHandlerMapping of the resource handler so that it resolves the static resource requests before controller classes get a chance

这篇关于Spring - 设置 HandlerMapping 优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆