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

放肆的年华 提交于 2019-12-31 05:31:10

问题


I've checked around for this problem, but after 4 hours of trying many things, nothing worked for me.

I get a 405 error when trying to access my css file. Here are my Config.java

package com.myapp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
@ComponentScan("com.myapp")
@EnableWebMvc
@EnableTransactionManagement
public class Config extends WebMvcConfigurerAdapter {

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/","/css/","/js/");

    }

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

}

and directories structure:

META-INF
WEB-INF
resources
    |-css
    |  |-myapp.css
    |-js
       |-myapp.js

Using netbeans as IDE. I've also try to out the resources directory in WEB-INF. Same results.

Edit: my default controller below

@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(ModelMap map) {
    map.put("msg", "Hello Spring 4 Web MVC!");
    return "index";
}

Edit 2: here is my WebinItalizer

public class WebInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {        
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();  
        ctx.register(Config.class);  
        ctx.setServletContext(servletContext);    
        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));  
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }
}

in this initializer, if I use "/" for servlet.addMapping("/") I can not have access to the css. But if I change it to something else like servlet.addMapping("/app") I can have access to the css. But the problem if I do this is all my url will start by app/ not very cool :/


回答1:


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).




回答2:


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.



来源:https://stackoverflow.com/questions/42162110/spring-4-static-content-like-css-js-brings-error-405-request-method-get-not-su

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