Navigator
Navigator

Reputation: 3121

Get Raw Html From the controller Spring thymeleaf

I have a controller that create model attribute and passes to the view "partial.html" to generate output

partial.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Home page</title>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<p>
    <span th:text="'Today is: ' + ${message}"></span>
</p>
</body>
</html>

and inside a controller method

model.addAttribute("message", search);

How to do I get Htlm Output to a string inside controller method? like this

String htmlOutput="from partial.html";

Upvotes: 2

Views: 7756

Answers (4)

MANOJ NIMAR
MANOJ NIMAR

Reputation: 126

Let's say you have a HTML file with two variable name and todayDate.
You want to process it and want to store it in a string / database / AWS S3.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        <p>Hello</p>
        <p th:text="${name}"></p>
        <p th:text="${todayDate}"></p>
    </body>
</html>

Your HTML file location is src/main/resources/templates/home.html

By using the below function you can get the final processed HTML as:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        <p>Hello</p>
        <p>Manoj</p>
        <p>30 November 2019</p>
    </body>
</html>
import org.thymeleaf.context.Context;

@GetMapping("/")
    public void process() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setPrefix("templates/");
        templateResolver.setCacheable(false);
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML");

        // https://github.com/thymeleaf/thymeleaf/issues/606
        templateResolver.setForceTemplateMode(true);

        templateEngine.setTemplateResolver(templateResolver);

        Context ctx = new Context();

        SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");
        Calendar cal = Calendar.getInstance();

        ctx.setVariable("todayDate", dateFormat.format(cal.getTime()));
        ctx.setVariable("name", "Manoj");
        final String result = templateEngine.process("home", ctx);
        System.out.println("result:" + result);
    }

Upvotes: 11

meszias
meszias

Reputation: 101

You may looking for this, getting directly the result HTML, just ignore the email part of the post.

Then, you can create this:

final Context ctx = new Context(locale);
ctx.setVariable("name", recipientName);
ctx.setVariable("subscriptionDate", new Date());
ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
ctx.setVariable("imageResourceName", imageResourceName); 
// so that we can reference it from HTML
final String htmlContent = this.templateEngine.process("html/email-inlineimage.html", ctx);

So you have the htmlContext of rendered thymeleaf template (with vars)

Upvotes: 3

user65839
user65839

Reputation:

If you're using the usual Spring MVC approach, as Joanna says you're doing things in the wrong order. The Controller creates the model and specifies the view, and then after that the view is rendered by the Thymeleaf template that uses the model.

If, on the other hand, you're trying to render Thymeleaf templates yourself (rather than sending them to the user's browser directly, maybe for use in HTML email or to store prerendered pages in a database or something), then you'd need to create your own Thymeleaf Template Engine to use. Refer to the "Creating and configuring the Template Engine" section of the documentation for details. You can create your own Engine, and then use its process method to get the result of the template to put into a variable for further use.

Upvotes: 4

so-random-dude
so-random-dude

Reputation: 16585

Once the control goes out to view processor (JSP/Thymeleaf etc), it will not be coming back to controller. You will be able to get the raw html response in a customFilter, but not in the Controller.

Upvotes: 1

Related Questions