Loading External Static HTML Files in your WAR

We added Solr Searching to NixMash Spring Posts and on the main search page provided some search tips. I wanted to be able to update those tips over time without having to update the application WAR in the process. The easiest way to do that was to load the tips as an external HTML resource.

The Web App is configured with Apache Proxy Module with file storage on a server location outside of the WAR. This NixMash Post describes the configuration if you want more info. That external resource area was where we would save our Search Tips HTML fragment.

How to Load the HTML Fragment

The interesting part, how to load the HTML fragment. We're using Thymeleaf, so th:include would've seemed an option if we could load resources from the file system. But we can't. And a soft-link under the /templates folder wouldn't work because the packaging of the WAR would have included the resources themselves. Yeah, yeah, we could have ignored the soft-link or something with Gradle, but the whole point was to load external content outside of the WAR file SIMPLY.

The solution was JQuery load(). This works across all browsers and platforms and meets our three criteria: 1) External location to WAR, 2) easy and simple to implement, and 3) content would be updated instantly without having to touch the WAR file. The “/files” directory is one of our Apache Alias locations not handled by our Proxy Module.

To complete the HTML.

Thymeleaf and Spring MVC Action

You'll notice the isSearchResult variable in both of the HTML excerpts. That's because the Search Tips fragment is only displayed on the initial Search Page HTTP {GET} Request, returned to the client with Spring MVC and handled by Thymeleaf. isSearchResult is TRUE on subsequent search requests. Here's the Controller method.

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.

Posted October 11, 2016 12:56 PM EDT

More Like This Post