Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 783
zeodtr Avatar asked Nov 19 '13 05:11

zeodtr


People also ask

Where do I put html files in spring boot?

html file should be placed under the templates directory and all JS and CSS files should be placed under the static directory in classpath. In the example shown, we used CSS file to change the color of the text. Now, we need to add the Spring Boot Starter Thymeleaf dependency in our build configuration file.

Can we use external Tomcat in spring boot?

the external configuration of your spring boot app on tomcat it could be a general expectation to read your configuration file from an external source where the it operation can handle it easily. to achieve this, you need to override the configure method from the mentioned class above in our main class.

How does spring boot serve static content?

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.


1 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 the 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.
like image 179
Dave Syer Avatar answered Oct 03 '22 11:10

Dave Syer