簡體   English   中英

Spring MVC沒有從webapp加載靜態資源

[英]Spring MVC not loading static resources from 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>

用於實例化一些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;
    }

}

當我點擊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,所以我不確定這是否有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM