Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}

forward is MvcConfig.java. As you can see, i add resourcehandler for static resources.

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, "/*");
}

And there is Initializer.java

There is my resource hierachy.

src
  -main
    --java
    --resources
    --webapp
      ---WEB-INF
         ----resources
             -----css
                  ------ signin.css
         ----views

In index.jsp,I called signin.css like this.

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

Then, i can found these error message.

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

In chrome browser, also has 405 error. [405-Error screen shot][1][1]: https://i.sstatic.net/vmDlk.png

How can i fix it?

like image 468
Jong Han Kim Avatar asked Jun 25 '26 07:06

Jong Han Kim


1 Answers

can you change

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

to

<link href="${pageContext.request.contextPath}/resources/css/signin.css"  rel="stylesheet">
like image 80
Nimesh Avatar answered Jun 26 '26 21:06

Nimesh