【发布时间】:2017-07-27 05:32:54
【问题描述】:
@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;
}
forward 是 MvcConfig.java。 如您所见,我为静态资源添加了资源处理程序。
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
我该如何解决?
【问题讨论】:
-
仅供参考,如果您使用的是 Boot,您可以将这些资源放在
src/main/resources/public(或static)下,您不必自己配置任何解析器。 -
我没有使用 Boot,我为 Spring legacy 创建 MVC 项目。然后我将 servlet 的配置更改为 Java 代码。
标签: javascript java css spring-mvc