Spring MVC无法从WebApp加载静态资源

无法使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>

用于Web应用程序实例化某些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>

回答:

好吧,我已经解决了这个问题,但还不确定为什么/如何做。该项目导入了一个jar,其中包含一个扩展WebMvcConfigurationSupport如下的类:

@Configuration

public class EnableUriMatrixVariableSupport extends WebMvcConfigurationSupport {

@Override

@Bean

public RequestMappingHandlerMapping requestMappingHandlerMapping() {

RequestMappingHandlerMapping hm = super.requestMappingHandlerMapping();

hm.setRemoveSemicolonContent(false);

return hm;

}

}

另外,我还具有@EnableMvc导入DelegatingWebMvcConfiguration的注释。我认为这最终会创建两个WebMvcConfigurationSupport实例,这会在spring容器中造成严重破坏。不幸的是,在解决此问题的过程中,我升级到了Spring

4.x,因此我不确定这是否有所帮助。

以上是 Spring MVC无法从WebApp加载静态资源 的全部内容, 来源链接: utcz.com/qa/399011.html

回到顶部