无法使用Spring MVC注释在我的Web应用程序中使用我的资源(例如图像,CSS等)。我什至使用了资源处理程序并给出了正确的路径。我什至尝试了多种方法,方法是使用classpath并直接给出路径而不使用它。但是没有任何效果。请帮帮我!
我的配置文件:
@EnableWebMvc
@ComponentScan({"com.dservlet"})
public class Config implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver vr= new InternalResourceViewResolver();
vr.setPrefix("");
vr.setSuffix(".jsp");
return vr;
}
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/").setCachePeriod(3600).resourceChain(true).addResolver(new PathResourceResolver());
//registry.addResourceHandler("/resources/css/**").addResourceLocations("/resources/css/").setCacheControl(CacheControl.maxAge(2, TimeUnit.HOURS).cachePublic());
//registry.addResourceHandler("/resources/scss/**").addResourceLocations("/resources/img/").setCacheControl(CacheControl.maxAge(2, TimeUnit.HOURS).cachePublic());
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}```
Dispatcher servlet:
```public class FrontController extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {Config.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}```
mapping file for dispatcher servlet:
```@Controller
public class MyController {
@GetMapping("/")
public String index(Model model) {
return("index");
}
}
网站控制台中的错误:
无法加载资源:服务器的状态为404()