@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
轉發是MvcConfig.java。如你所見,我爲靜態資源添加資源處理程序。爲什麼HTTP 405錯誤在Spring-MVC 4中加載css/js?
private void addDispatcherServlet(ServletContext servletContext)
{
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.getEnvironment().addActiveProfile("production");
applicationContext.register(MvcConfig.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(applicationContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
dispatcher.setInitParameter("dispatchOptionsRequest", "true");
}
private void addUtf8CharacterEncodingFilter(ServletContext servletContext)
{
FilterRegistration.Dynamic filter = servletContext.addFilter("CHARACTER_ENCODING_FILTER", CharacterEncodingFilter.class);
filter.setInitParameter("encoding", "UTF-8");
filter.setInitParameter("forceEncoding", "true");
filter.addMappingForUrlPatterns(null, false, "/*");
}
而且有Initializer.java
還有就是我的資源層次結構。
src
-main
--java
--resources
--webapp
---WEB-INF
----resources
-----css
------ signin.css
----views
在index.jsp中,我像這樣調用了signin.css。
<link href="/resources/css/signin.css" rel="stylesheet">
然後,我可以找到這些錯誤信息。
WARN [2017-03-07 14:37:49] ({http-bio-8080-exec-14} DefaultHandlerExceptionResolver.java[handleHttpRequestMethodNotSupported]:215) - Request method 'GET' not supported
WARN [2017-03-07 14:37:49] ({http-bio-8080-exec-15} DefaultHandlerExceptionResolver.java[handleHttpRequestMethodNotSupported]:215) - Request method 'GET' not supported
在Chrome瀏覽器中,也有405錯誤。 [405-錯誤截圖] [1] [1]:https://i.stack.imgur.com/vmDlk.png
我該如何解決?
僅供參考,如果你使用的引導,就可以把這些資源下'的src/main/resources/public'(或'static'),你不必自己配置任何解析器。 – chrylis
我沒有使用Boot ,,,我爲Spring遺留創建了MVC項目。然後我將servlet的配置更改爲Java代碼。 –