Spring 5静态资源映射

问题描述 投票:0回答:1

我正在迁移到Spring5。我将所有xml都移到了Java配置中,但是我停留在静态资源上。

在我的旧xml配置中,我的资源定义如下:<mvc:resources mapping="/3rdparty/**" location="/3rdparty/" />

将它们添加到我的Java Config类中

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.mypackages.*")
public class WebConfig implements WebMvcConfigurer {


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/3rdparty/**").addResourceLocations("/3rdparty/");


    }

     @Bean
       public ViewResolver viewResolver() {
          InternalResourceViewResolver bean = new InternalResourceViewResolver();

          bean.setPrefix("/");
          bean.setSuffix(".jsp");

          return bean;
       }



}

现在我的资源出现这些错误No mapping for GET /myapp/gui/3rdparty/bootstrap-3.3.6-dist/js/bootstrap.min.js

与xml配置都很好用。任何有关如何定义模式的想法。

文件夹结构是这样

ROOT
 |_WEB-INF
 |_META-INF
 |_gui
   |_3rdparty

我也将启动项移至Java配置

public class AppInitializer implements WebApplicationInitializer{

     @Override
        public void onStartup(ServletContext container) {

             AnnotationConfigWebApplicationContext context  = new AnnotationConfigWebApplicationContext();
             context.setConfigLocation("com.mypackages.config");

             container.addListener(new ContextLoaderListener(context));


            ServletRegistration.Dynamic dispatcher = container
              .addServlet("dispatcher", new DispatcherServlet(context));

            dispatcher.setLoadOnStartup(1);
            dispatcher.addMapping("/");
        }
}

提前感谢您的帮助

spring spring-mvc spring-java-config
1个回答
0
投票

我建议更改两件事:

© www.soinside.com 2019 - 2024. All rights reserved.