繁体   English   中英

为什么在Spring-MVC 4中加载css / js会出现HTTP 405错误?

[英]Why HTTP 405 error for loading css/js in Spring-MVC 4?

@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。 如您所见,我为静态资源添加了resourcehandler。

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

我该如何解决?

你能改变吗?

<link href="/resources/css/signin.css"  rel="stylesheet">

<link href="${pageContext.request.contextPath}/resources/css/signin.css"  rel="stylesheet">

试试这个

registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM