【发布时间】:2014-10-15 22:09:28
【问题描述】:
问题 无法让 Spring 为静态资源添加资源处理程序。
背景信息 我有一个在独立 Jetty 实例中运行的 Spring MVC webapp。我的 webapp 结构是
webapp root/
resources/
css/
images/
js/
WEB-INF/
layouts/
tiles.xml
page.jsp
lib/
views/
home/
home.jsp
tiles.xml
web.xml
我已扩展 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/");
}
}
我的小控制器是这样的:
@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");
}
}
我的 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>
我的 webapp 用于实例化一些 mvc 解析器的 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;
}
}
当我点击 url company.com/myservlet/home 时,瓷砖的东西可以工作,并且会显示 home.jsp 页面,但没有加载 css 和图像。 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']
我尝试过调试 servlet,但从未调用过 WebMvcConfig.addResourceHandlers() 方法,因此无法找到静态资源。让这个工作我缺少什么?
作为一个注释,如果我将以下内容添加到我的 web.xml 中,css 资源将被加载,但我想知道为什么不调用 WebMvcConfig.addResourceHandlers() 方法,所以我不需要这个 servlet 映射。
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
【问题讨论】:
-
我看不出你的配置有什么问题。
DelegatingWebMvcConfiguration应该能够在setConfigurers()中检测到您的WebMvcConfigurerAdapter。 -
@Shane Rowatt this 服务于您的目的,然后我发布此链接作为您接受的答案。
-
资源URL生成好了吗?因为您的配置看起来很好,但您只映射
/resources/**(这是正常的)。但是 PageNotFound 是针对/api/v1/tlb/resources/form.css的。你试过直接在浏览器中输入网址吗? -
你的配置看起来不错。您如何在 .jsp 中添加资源?请发布一个例子。
标签: java spring spring-mvc servlets