El contenido estático de Spring 4 como css / js trae el error 405 El método de solicitud 'GET' no es compatible
He revisado este problema, pero después de 4 horas de intentar muchas cosas, nada funcionó para mí.
Recibo un error 405 cuando intento acceder a mi archivo css. Aquí están mis 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();
}
}
y estructura de directorios:
META-INF
WEB-INF
resources
|-css
| |-myapp.css
|-js
|-myapp.js
Usando netbeans como IDE. También he intentado sacar el directorio de recursos en WEB-INF. Los mismos resultados
Editar: mi controlador predeterminado a continuación
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(ModelMap map) {
map.put("msg", "Hello Spring 4 Web MVC!");
return "index";
}
Edición 2: aquí está mi 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);
}
}
en este inicializador, si uso "/" para servlet.addMapping ("/") no puedo tener acceso al css. Pero si lo cambio a algo más como servlet.addMapping ("/ app") puedo tener acceso al css. Pero el problema si hago esto es que mi url comenzará por la aplicación / no muy bueno: /