Home > OS >  How to render HTML without re-running app
How to render HTML without re-running app

Time:08-16

In IntelliJ IDEA, I have a back-end project build with Spring Boot and there is a PDF generator that uses a static HTML content. When I make a change on HTML file, I am expecting these changes would reflect to the next generated PDF files. However, every time it needs to be restarted.

I use "Build project automatically" on the Compiler settings and the following dependency (Spring Boot Dev Tools):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

Also run the app with debug mode, but nothing is changed. Is it possible to make the changes on the HTML would reflect to the generated PDF file?

CodePudding user response:

The issue or expected behavior is that when you are deploying your application, everything is getting packed into .war for example. So, that current code, HTML, Java, XML, or anything else is packed into that war and it is deployed.

Even if you make any other changes to HTML, Java, or XML, the war that is already deployed cannot know about those changes until you rebuilt your application or redeployed it.

I think of that war as a train, it is on the station, people get it, and the train takes off. There is no possible way that the train will know who is at the station that it took off a few minutes ago.

What could cause the changes to be visible, is that if you place that whole HTML content into your database, and once you make any changes to it, you persist those changes and after everything is saved ( still talking about that HTML ), the application will fetch that HTML which you already updated from the database. Because from that perspective, there is no redeployment required, the only thing you need is to read the content from the database.

As for your development environment I created a very basic spring boot project with one html page called helloworld.html which is located in src/main/resources/templates/helloworld.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body> 
<p>Hello worldz</p>
<p th:text="'Time on server is ' ${theDate}" />

</body>
</html>

And a Controller

package com.springboot.thymeleafdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class DemoController {
 
    @GetMapping("/hello")
    public String sayHello(Model theModel) {
        theModel.addAttribute("theDate",new java.util.Date());
        return "helloworld";
    }
        
}

With dependency for devtools

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>

After doing some changes to html and saving, nothing happened, after adding changes to controller, application would reload the changes, but only for java class, the HTML would remain in state right before application was launched. But after adding this in application.property

spring.devtools.restart.exclude=static/**,public/**

The application started to reload and reflect the changes on the HTML as well, it does not happen "automatically" tho, you still need to hit the reload button in your browser for this example.

Please keep in mind that this is possible only in your development environment, when it comes to packing your application into a war, the train theory remains the same.

My suggestion is to try to create a small app as well like this one and test what I shared, it is easiest to find the solution in tiny projects compared to 200 lines of pom.xml, because from what I can see, it is triggering devtools to work with static HTML as well in your IDE, but I still believe that reflecting only changes on HTML would not dynamically propagate changes to PDF.

  • Related