package net.educoder.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration //@EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { /******************begin****************/ /******************end******************/ } } 补充代码,配置 html 目录文件在 /html/ 路径 下
时间: 2023-06-29 19:10:16 浏览: 51
可以在 addResourceHandlers 方法中添加以下代码来配置 html 目录文件在 /html/ 路径下:
```
registry.addResourceHandler("/html/**")
.addResourceLocations("classpath:/html/");
```
其中,`addResourceHandler` 指定了 URL 请求路径的规则,`addResourceLocations` 指定了对应的文件路径。在本例中,`/html/**` 表示所有以 `/html/` 开头的请求都会被映射到 `classpath:/html/` 目录下寻找对应的文件。
相关问题
package com.aichen.socketwebchat.config; import org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; @Configuration public class SocketConfig extends WebSocketMessagingAutoConfiguration { @Bean public ServerEndpointExporter serverEndpointExporter(){ return new ServerEndpointExporter(); } }
这是一个关于 WebSocket 的配置文件,其中定义了一个 ServerEndpointExporter 的 Bean。这个 Bean 可以将带有 @ServerEndpoint 注解的类注册为 WebSocket 端点。
package com.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import com.interceptor.AuthorizationInterceptor; @Configuration public class InterceptorConfig extends WebMvcConfigurationSupport{ @Bean public AuthorizationInterceptor getAuthorizationInterceptor() { return new AuthorizationInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(getAuthorizationInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**"); super.addInterceptors(registry); } /** * springboot 2.0配置WebMvcConfigurationSupport之后,会导致默认配置被覆盖,要访问静态资源需要重写addResourceHandlers方法 */ @Override 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); } }
这是一个 Java Spring 的配置类,用于配置拦截器和静态资源访问路径。其中,@Configuration 注解表示这是一个配置类,@Bean 注解表示该方法返回一个 Bean 对象,getAuthorizationInterceptor() 方法返回一个 AuthorizationInterceptor 对象,用于验证用户登录状态。addInterceptors() 方法用于添加拦截器,这里将 AuthorizationInterceptor 对象添加到所有路径上,但排除了 /static/ 路径。addResourceHandlers() 方法用于配置静态资源访问路径,这里将 classpath:/resources/、classpath:/static/、classpath:/admin/、classpath:/front/、classpath:/public/ 这几个路径下的静态资源都配置为可访问的。