Search code examples
springspring-mvctomcatamazon-ec2static-content

How to serve static data from outside war file in spring mvc maven application with tomcat


Hi I had created a maven web application with spring mvc framework, I had created war file also deploy in amazon web server because of lots of static files like bootstrap css and js file and other, the war file is heavy and its taking too much time to load first page. the war file is bloated, how to separate out static files, such as bootstrap from your war? how do I resolve this problem?


Solution

  • because of lots of static files like bootstrap css and js file and other, the war file is heavy and its taking too much time to load first page I'm not sure that heavy war file is a reason why a page is loading slow. There are tons of reasons that can cause such behavior. I'd suggest to look closer on that first.

    how to separate out static files, such as bootstrap from your war?

    Spring MVC can be configured to serve static assets from a file system:

    Here is an example:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
          .addResourceHandler("/files/**")
          .addResourceLocations("file:/opt/files/");
     }
    

    More examples and explanation can be found here: http://www.baeldung.com/spring-mvc-static-resources

    how do I resolve this problem?

    I'm using public CDN servers (https://maxcdn.bootstrapcdn.com, https://cdnjs.cloudflare.com, https://cdn.rawgit.com, https://yandex.st) for the static resources like Bootstrap, jQuery, etc.