public void addResourceHandlers(ResourceHandlerRegistry registry){ registry.
时间: 2024-05-30 13:16:12 浏览: 141
addResourceHandler("/static/**").addResourceLocations("/resources/static/"); }
This method adds a resource handler for the "/static/**" URL pattern, which maps to the "/resources/static/" directory in the application's classpath. This allows static resources such as images, CSS files, and JavaScript files to be served to the client. The ResourceHandlerRegistry is used to register the resource handler with the Spring MVC framework.
相关问题
public void addResourceHandlers(ResourceHandlerRegistry registry) { //访问路径 registry.addResourceHandler("/**") //映射真实路径 .addResourceLocations("classpath:/resources/") .addResourceLocations("classpath:/static/") .addResourceLocations("classpath:/admin/") .addResourceLocations("classpath:/front/") .addResourceLocations("classpath:/public/"); super.addResourceHandlers(registry); }这段代码是什么意思
这段代码是为了配置Spring MVC应用程序的资源处理器,它指定了静态资源的访问路径及其真实存储路径。具体而言,`addResourceHandler("/**")`指定了所有的请求路径都可以访问静态资源,`.addResourceLocations("classpath:/resources/")`指定了静态资源的存储路径为`classpath:/resources/`,`.addResourceLocations("classpath:/static/")`指定了静态资源的存储路径为`classpath:/static/`,类似地,`.addResourceLocations("classpath:/admin/")`、`.addResourceLocations("classpath:/front/")`和`.addResourceLocations("classpath:/public/")`分别指定了不同类型的静态资源的存储路径。
当浏览器请求某个静态资源时,Spring MVC会根据请求路径通过资源处理器去查找对应的静态资源,然后返回给浏览器。`super.addResourceHandlers(registry)`表示将资源处理器注册到Spring MVC配置中,以便生效。
public class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("spash.html"); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/upload/**").addResourceLocations("file:D:\\bishe\\qiangjunstudypic/"); //静态资源释放 registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/","classpath:/static/**","classpath:/templates/"); }
这段代码是一个Spring MVC的配置类,实现了WebMvcConfigurer接口,用于配置视图控制器和静态资源处理器。
其中addViewControllers方法用于注册一个视图控制器,将访问根路径"/"的请求映射到名为"spash.html"的视图上。
addResourceHandlers方法用于注册一个静态资源处理器,将以"/upload"开头的请求映射到指定的本地文件目录下,将以"/"开头的请求映射到classpath下的静态资源文件目录下,这些静态资源可以是html、css、js、图片等。
阅读全文