Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat serving static resources on Spring MVC app

I'm building a Spring MVC application, and the frontController servlet is mapped in "/" intercepting all requests, I'd to be able to serve the static contents (.js,.css,.png...) from tomcat and not by Spring. My app structure is

-webapp/
   styles/
   images/
   WEB-INF/
          views/

By default, because the frontController is mapped on the context root of my app its handles all requests but don't serve any static resource. The mvc configurarion for static resources is follow.

<mvc:resources mapping="/resources/**" location="/"/>

And the page's code is:

<img src="resources/images/logo.png" />

I need to configure Tomcat to serve the static resources with no spring interaction.

Any suggestion?

like image 783
Rigoni Avatar asked Sep 18 '10 23:09

Rigoni


People also ask

Which bean serve static resources in spring?

Spring Boot comes with a pre-configured implementation of ResourceHttpRequestHandler to facilitate serving static resources. By default, this handler serves static content from any of the /static, /public, /resources, and /META-INF/resources directories that are on the classpath.

Where do static files go in spring boot?

The file is located in the src/main/resources/static directory, which is a default directory where Spring looks for static content. In the link tag we refer to the main. css static resource, which is located in the src/main/resources/static/css directory. In the main.

Which module is used to serve static resources in Node JS?

In your node application, you can use node-static module to serve static resources. The node-static module is an HTTP static-file server module with built-in caching.

What is @EnableWebMvc?

The @EnableWebMvc annotation is used for enabling Spring MVC in an application and works by importing the Spring MVC Configuration from WebMvcConfigurationSupport. The XML equivalent with similar functionality is <mvc:annotation-driven/>.


1 Answers

You can remap tomcats default servlet (which handles static content), e.g.

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/images/*</url-pattern>
</servlet-mapping>
like image 130
nos Avatar answered Oct 17 '22 15:10

nos