[英]Spring MVC not loading static resources from webapp
Issue Unable to get Spring to add a resource handler for static resources. 问题无法让Spring为静态资源添加资源处理程序。
Background Information I have a Spring MVC webapp running in a standalone Jetty instance. 背景信息我在一个独立的Jetty实例中运行了一个Spring MVC webapp。 My webapp structure is
我的webapp结构是
webapp root/
resources/
css/
images/
js/
WEB-INF/
layouts/
tiles.xml
page.jsp
lib/
views/
home/
home.jsp
tiles.xml
web.xml
I've extended the WebMvcConfigurerAdaptor
to add a resource mapping such that urls like company.com/myservlet/resources/css/main.css
will get resolved to the webapp root/resources/css
folder. 我已经扩展了
WebMvcConfigurerAdaptor
以添加资源映射,以便像company.com/myservlet/resources/css/main.css
这样的URL将被解析为webapp root/resources/css
文件夹。
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
My little controller is this: 我的小控制器是这样的:
@Controller
public class SpringAppController {
public SpringAppController() {
}
@RequestMapping(value = "/home", method = RequestMethod.GET)
public ModelAndView handleRequestHome(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
return new ModelAndView("home");
}
}
My web.xml is simply this: 我的web.xml就是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Java-based Spring container definition -->
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<!-- Location of Java @Configuration classes that configure the components that makeup this application -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.company</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>admin</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>admin</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Ensure UTF-8 encoded pages so that certain characters are displayed and submitted correctly -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Enables support for DELETE and PUT request methods with web browser clients -->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
My JavaConfig for the webapp to instantiate some mvc resolvers is this: 用于实例化一些mvc解析器的webapp的JavaConfig是这样的:
@Configuration
public class MainConfig {
@Bean
public ViewResolver viewResolver() {
UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
viewResolver.setViewClass(TilesView.class);
return viewResolver;
}
@Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer configurer = new TilesConfigurer();
configurer.setDefinitions(new String[] {
"/WEB-INF/layouts/tiles.xml",
"/WEB-INF/views/**/tiles.xml"
});
configurer.setCheckRefresh(true);
return configurer;
}
@Bean
public org.springframework.context.MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("/WEB-INF/messages/messages");
return messageSource;
}
}
When I hit the url company.com/myservlet/home the tiles stuff works and the home.jsp page is displayed but the css and images are not loaded. 当我点击url company.com/myservlet/home时,磁贴工作正常,并显示home.jsp页面,但未加载css和图像。 The servlet logs this warning:
servlet记录此警告:
[DEBUG] [DispatcherServlet] [DispatcherServlet with name 'admin' processing GET request for [/api/v1/tlb/resources/page.css]]
[DEBUG] [RequestMappingHandlerMapping] [Looking up handler method for path /resources/form.css]
[DEBUG] [RequestMappingHandlerMapping] [Did not find handler method for [/resources/form.css]]
[WARN ] [PageNotFound] [No mapping found for HTTP request with URI [/api/v1/tlb/resources/form.css] in DispatcherServlet with name 'admin']
I've tried debugging the servlet and the WebMvcConfig.addResourceHandlers()
method is never called and hence why the static resources cannot be found. 我试过调试servlet,从不调用
WebMvcConfig.addResourceHandlers()
方法,因此无法找到静态资源。 What am I missing to get this working? 我错过了什么让这个工作?
As a note if I add the following to my web.xml the css resource will be loaded but I would like to know why the WebMvcConfig.addResourceHandlers()
method is not called so I don't need this servlet mapping. 作为一个注释,如果我将以下内容添加到我的web.xml中,将加载css资源,但我想知道为什么不调用
WebMvcConfig.addResourceHandlers()
方法,所以我不需要这个servlet映射。
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
Well, I've fixed the issue but not exactly sure why/how yet. 好吧,我已经解决了这个问题,但还不确定为什么/如何。 The project had imported a jar that contained a class that extended
WebMvcConfigurationSupport
like the following: 该项目导入了一个jar,其中包含一个扩展
WebMvcConfigurationSupport
类,如下所示:
@Configuration
public class EnableUriMatrixVariableSupport extends WebMvcConfigurationSupport {
@Override
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping hm = super.requestMappingHandlerMapping();
hm.setRemoveSemicolonContent(false);
return hm;
}
}
Additionally I also has @EnableMvc
annotation which imports DelegatingWebMvcConfiguration. 另外我还有
@EnableMvc
注释,它导入DelegatingWebMvcConfiguration。 I think this ends up creating two instances of a WebMvcConfigurationSupport and this causes havoc in the spring container. 我认为这最终会创建两个WebMvcConfigurationSupport实例,这会导致spring容器中的混乱。 Unfortunately I upgraded to spring 4.x in the process of fixing this issue so I'm not sure yet if this helped in some way.
不幸的是,我在修复这个问题的过程中升级到了spring 4.x,所以我不确定这是否有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.