Spring 4 static content like css/js brings error 405 Request method 'GET' not supported

☆樱花仙子☆ 提交于 2019-12-02 09:01:54

First, if this is incorrect, sorry. I do a lot of Spring, but it's typically only XML configuration.

Let's start here:

     registry.addResourceHandler("/resources/**")
             .addResourceLocations("/resources/","/css/","/js/");

This is saying "when I request something with the path http://localhost/resources/** look in /resources/, /css/ or /js/.

Well, /css/ and /js/ don't exist. Only /resources/ does.

In this case, you should map this

registry.addResourceHandler("/resources/**")
                 .addResourceLocations("/resources/");

And access this way:

<script src="/resources/js/myapp.js"></script>

Alternately, you could just do this:

registry.addResourceHandler("/js/**")
                 .addResourceLocations("/resources/js/");
registry.addResourceHandler("/css/**")
                 .addResourceLocations("/resources/css/");

and access the static content this way:

<script src="/js/myapp.js"></script>

As for why changing the context root worked, it's due to this:

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

This attempts to route static content requests through the default servlet with the context root /. Since you're creating your DispatcherServlet at /, the default servlet will never be hit since it's set to the lowest priority by default.

If you're using registry.addResourceHandler you do not need to configure the default servlet and vice versa. As you discovered, you never hit it as by default it's the lowest priority.

If you still have issues, even after fixing the ResourceHandler, you can try omitting the resource handler configuration completely, or setting the default servlet to a higher priority (Which I wouldn't recommend).

The following solution worked for me. I added resource handlers to my configuration class which extends WebMvcConfigurerAdapter.

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/bootstrap/**")
                .addResourceLocations("/bootstrap/");
    }

Then, I imported my bootstrap.min.js file in my jsp as shown below :

<script type="text/javascript" src="<c:url value="/bootstrap/js/bootstrap.min.js"/>" ></script>

Note that bootstrap folder specified in the Resourcehandler resides under WebContent folder.

Edit :

After looking at your code in GitHub, I can clearly see that your resource handler has an extra * in it. It should look like below :

Clearly i don't see that you used <c:url> in your jsp. Do the following things and see if it works :

  1. Remove the extra * from resource handler to look like below :

registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");

  1. In your index.jsp file, change the link and script tags to look like this:

<script type="text/javascript" src="<c:url value="/resources/js/js.js"/>" ></script>
<link rel="stylesheet" href="<c:url value="/resources/css/site.css"/>"  type="text/css">

Do not use context path ${cp} for script and link tags anymore. <c:url> is the reason this worked for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!