像css / js这样的Spring 4静态内容会带来错误405请求方法’GET’不受支持

我已经检查了这个问题,但经过4个小时的尝试很多事情后,对我来说没什么用。

尝试访问我的css文件时出现405错误。 这是我的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(); } } 

和目录结构:

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

使用netbeans作为IDE。 我也尝试在WEB-INF中输出资源目录。 结果相同。

编辑:我的默认控制器如下

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

编辑2:这是我的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); } } 

在这个初始化程序中,如果我使用“/”作为servlet.addMapping(“/”),我就无法访问css。 但是,如果我将其更改为servlet.addMapping(“/ app”)之类的其他内容,我可以访问css。 但问题是,如果我这样做是我的所有url将由app开始/不是很酷:/

首先,如果这不正确,抱歉。 我做了很多Spring,但它通常只是XML配置。

我们从这里开始:

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

这就是说“当我通过http://localhost/resources/**请求某些内容时,请查看/resources//css//js/

好吧, /css//js/不存在。 只有/resources/

在这种情况下,您应该映射这个

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

并以这种方式访问​​:

或者,您可以这样做:

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

并以这种方式访问​​静态内容:

至于为什么更改上下文根工作,这是由于:

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

这会尝试通过具有上下文根/的默认servlet路由静态内容请求。 由于您在/创建DispatcherServlet,因此默认servlet将永远不会被命中,因为它默认设置为最低优先级。

如果您正在使用registry.addResourceHandler ,则无需配置默认servlet,反之亦然。 正如您所发现的那样,您从未打过它,因为默认情况下它是最低优先级。

如果您仍然遇到问题,即使在修复ResourceHandler之后,您也可以尝试完全省略资源处理程序配置,或者将默认servlet设置为更高的优先级(我不建议这样做)。

以下解决方案适合我。 我在配置类中添加了资源处理程序,扩展了WebMvcConfigurerAdapter

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

然后,我在我的jsp中导入了我的bootstrap.min.js文件,如下所示:

 " > 

请注意,Resourcehandler中指定的引导文件夹位于WebContent文件夹下。

编辑:

在GitHub中查看您的代码之后,我可以清楚地看到您的资源处理程序中有一个额外的* 。 它应该如下所示:

很明显,我没有看到你在你的jsp中使用了 。 做以下事情,看看它是否有效:

  1. 从资源处理程序中删除额外的*,如下所示:

。registry.addResourceHandler( “/资源/ **”)addResourceLocations( “/资源/”);

  1. index.jsp文件中,将链接和脚本标记更改为如下所示:
 " > " type="text/css">