How to service external static HTML files in Spring Boot Embedded tomcat?

By : zeodtr
Source: Stackoverflow.com
Question!

I'm new to Spring framework and Spring Boot.
I've implemented a very simple RESTful Spring Boot web application.
You can see the nearly full source code in another question: Spring Boot: How to externalize JDBC datasource configuration?

How can the app service external static HTML, css js files?
For example, the directory structure may be as follows:

MyApp\
   MyApp.jar (this is the Spring Boot app that services the static files below)
   static\
       index.htm
       images\
           logo.jpg
       js\
           main.js
           sub.js
       css\
           app.css
       part\
           main.htm
           sub.htm

I've read the method to build a .WAR file that contains static HTML files, but since it requires rebuild and redeploy of WAR file even on single HTML file modification, that method is unacceptable.

An exact and concrete answer is preferable since my knowledge of Spring is very limited.

By : zeodtr


Answers
I see from another of your questions that what you actually want is to be able to change the path to static resources in your application from the default values. Leaving aside the question of why you would want to do that, there are several possible answers. One is that you can provide a normal Spring MVC @Bean of type WebMvcConfigurerAdapter and use the addResourceHandlers() method to add additional paths to static resources (see WebMvcAutoConfiguration for the defaults). Another approach is to use trhe ConfigurableEmbeddedServletContainerFactory features to set the servlet context root path. The full "nuclear option" for that is to provide a @Bean definition of type EmbeddedServletContainerFactory that set up the servlet container in the way you want it. If you use one of the existing concrete implementations they extend the Abstract* class that you already found, so they even have a setter for a property called documentRoot. You can also do a lot of common manipulations using a @Bean of type EmbeddedServletContainerCustomizer.

By : Dave Syer


Is enough if you specify '-cp .' option in command 'java -jar blabla.jar' and in current directory is 'static' directory

By : pper


This video can help you solving your question :)
By: admin