Thymeleaf cache set to false not working - spring

I have a spring application under development.
The configuration of the project is as follows:
spring-dev-tools dependency is one of the maven dependencies
thymeleaf dependency is added through spring-boot-starter, that is org.springframework.boot:spring-boot-starter-thymeleaf
I have explicitly disabled template cache with spring.thymeleaf.cache=false
The src/main/resources folder is marked as resource in my IntelliJ IDEA project.
I am using IntelliJ IDEA project to run the application.
When I run the application, I can see in the console output that: LiveReload server is running on port 35729
Although, it seems like all my configuration is correct, the running application is not loading any changes I made to the templates while it is running.

If you are using Spring Tool Suite 4, you need to implement the following in your application.properties to disable the cache from Thymeleaf:
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=file:src/main/resources/templates/
Adding the spring.thymeleaf.prefix value worked for me.
For more information about the solution:
https://github.com/spring-projects/spring-boot/issues/34#issuecomment-316295791

The LiveReload server loads any changes only when one of the files on the classpath is modified Spring Doc.
Though I have resources, the folder containing templates folder, on the classpath, the changes to the html files in templates folder are not reflected in the running application.
What worked for me is to also add src/main/resources/templates to the classpath i.e. marked as resource in IntelliJ
Thanks

Related

Why wont this Gradle based Spring Boot project serve static resources when running within IntelliJ

Whilst using IntelliJ Idea, I´ve come across a difference between creating a practically identical Spring Boot Initializr project for Maven Project and the same but for a Gradle Project.
When creating and running the Maven based project, and adding a simple index.html to src/main/resources/static, accessing localhost:8080 in the browser shows the index.html as expected.
However when attempting exactly the same but with a Gradle based project, it yields a Whitelabel Error Page, due to a 404 not found error.
I also noticed in the output, the Maven project does mention
o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
whereas this is not showing in the output from the Gradle version.
Can anyone help confirm whether this issue an issue with IntelliJ and whether I need to further configure the Gradle params in order to 'see' and serve the static resources.
Note the bootRun gradle task works as expected, so it does look like it´s the way Intellij launches the project. it doesn´t package up the resources folder.
Thanks.

JSPs, static content, SpringBoot jars and IntelliJ

So I'm developing in IntelliJ a spring boot app. Using Gradle, I'm creating the sprint boot jar file.
I'm having problems figuring out where to put the jsps and static content such as .js files in such a way that running the jar AND running from within IntelliJ works!
It seems that in order to get SpringBoot to find jsps in a jar file I need to put the jsps inside a src/main/resources/META-INF/resources directory. For example, META-INF/resources/WEB-INF/index.jsp. I'm pretty sure the WEB-INF is now meaningless.
However, if I try to run this spring boot app from within IntelliJ, it cannot find the jsp. 404, blah blah blah. I actually have to put the jsps in the war-style webapp directory in src/main. However that directory is totally ignored during the spring boot jar build.
So.. how do developers set up their development environments that is both IntelliJ and, say, gradle bootRun friendly?
There is a guide here which should work both in IntelliJ IDEA and in the command line via the war file which can be executed as a jar (via java -jar).
As per spring boot there are Limitations when it comes to jsp's.
To overcome these limitations we need to have the configuration made in the application to render jsp by placing the jsp's under src/main/resources/META-INF/resources/WEB-INF/jsp folder.
Sample Code: Click Here
References:
https://dzone.com/articles/spring-boot-with-jsps-in-executable-jars-1
https://github.com/hengyunabc/spring-boot-fat-jar-jsp-sample

How to create deployable springboot war

I am trying to create a maven spring boot application to be deployed in Tomcat. I am following what is suggested in Spring docs and other stackoverflow suggestions- war, Application.java extending SpringBootServletInitializer, removing spring-boot-maven-plugin from build plugins etc. War file is generated and is deployed in tomcat. But what I found is all static files are packaged under /WEB-INF/classes folder and I am not able to access the page. My project structure is as below:
Can anybody tell me how I can package the war properly to be deployed in Tomcat.
That doesn't change anything.
If you put your static assets in src/main/resources/static (and they end up in WEB-INF/classes/static), Spring Boot will serve them properly. So a src/main/resources/static/foo.png will be available at http://localhost:8080/your-context/foo.png if the context of your webapp is your-context.
Regarding the configuration, you can also go on start.spring.io, click advanced and chose war and you'll get an empty project pre-configured.
Or you can click this: https://start.spring.io/#!packaging=war
The issue is because of version issue. I compiled the application with Java 8 and deployed it in tomcat running under JRE 7. It may help someone facing the same issue.
I got the clue from the below post:
Spring boot war file deploy on Tomcat.

Spring Boot Gradle - avoid lib-provided folder in war file

I have a Spring Boot based application and I'm trying to switch over from Maven to Gradle. The application is supposed to build a war file, which is deployed to a web server (WildFly in our case).
Now, I have some libraries provided by the web server and thus using a "providedCompile" scope (For hibernate search and infinispan). Now, when used with Spring Boot plugin, the plugin is creating the war file with all the "providedCompile" libraries moved to a folder named "lib-provided".
How do I avoid this? On the same context, it is also adding the Spring Boot loader classes on to the war file. If possible, I need to avoid this too.
Please help! Thanks!
If you're only ever going to deploy your application as a WAR file to an app server, then you don't need it to be turned into an executable archive. You can disable this repackaging in your build.gradle file:
bootRepackage {
enabled = false
}

IntelliJ + Tomcat + Spring-Loaded

I would like to try out Spring Source's "Spring Loaded" class reloading agent, with Tomcat run via IntelliJ.
https://github.com/SpringSource/spring-loaded
I've added the JVM arguments to my Tomcat run configuration, and my webapp starts up without errors and seems to behave normally.
I'm not really sure how to trigger the class reloading though. Do I just need to compile the classes that I modify? I've tried that and that didn't seem to work. Do I need to update Tomcat and deploy classes and resources? That doesn't seem to work either...?
Any specific configuration details would be greatly appreciated.
EDIT: More info, I think my problem may have to do with using two modules, one a core code library, and one the webapp. I use Maven for both, one configured as a jar project and the other as a war project. IntelliJ sets up the Artifact for the exploded war to use the jar module's jar file. I've tried switching the Artifact config to incorporate the jar module's compiled output into WEB-INF/classes instead. Now I see the .class files in target/webapp/WEB-INF/class/etc. But I'm not sure how to get IntelliJ to update a specific class file when I edit it.
If you added the following jvm parameters (as described at the springloaded page) to your tomcat
-javaagent:<pathTo>/springloaded-{VERSION}.jar -noverify SomeJavaClass
you should be fine. It may help to disable auto reloading in tomcat for the webapp you are testing. For exmaple in your server.xml:
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Context docBase="projekt" path="/projekt" reloadable="false" ...
This ensures that only the springloaded classloader loads changed classes.
spring loaded cannot hotswap jars.
Put web fragment output into webapp/WEB-INF/classes
Also spring loaded support only spring 4.1
https://github.com/spring-projects/spring-loaded/issues/139

Resources