Conteúdo estático do Spring 4, como css / js, traz o erro 405 Método de solicitação 'GET' não suportado
Eu verifiquei esse problema, mas depois de 4 horas tentando muitas coisas, nada funcionou para mim.
Eu recebo um erro 405 ao tentar acessar meu arquivo css. Aqui está o meu 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();
}
}
e estrutura de diretórios:
META-INF
WEB-INF
resources
|-css
| |-myapp.css
|-js
|-myapp.js
Usando netbeans como IDE. Também tentei sair do diretório de recursos no WEB-INF. Mesmos resultados.
Edit: meu controlador padrão abaixo
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(ModelMap map) {
map.put("msg", "Hello Spring 4 Web MVC!");
return "index";
}
Edit 2: aqui está o meu 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);
}
}
neste inicializador, se eu usar "/" para servlet.addMapping ("/"), não poderei ter acesso ao css. Mas se eu mudar para algo como servlet.addMapping ("/ app"), posso ter acesso ao css. Mas o problema se eu fizer isso é que todo o meu URL começará pelo aplicativo / não é muito legal: /