Home > Net >  Convert Model variable to a Context variable in SpringBoot
Convert Model variable to a Context variable in SpringBoot

Time:01-29

This is what I already have :

model.addAttribute("var1", var1);
model.addAttribute("var99", var99);

This is what I have to do - so its a complete re-do :

Context ctx = new Context();

ctx.setVariable("var1", var1);
ctx.setVariable("var99", var99);

Is there a way to set model to ctx ?

What I really want to do :

String htmlTemplate = myTemplateEngine.process("html/foo.html", ctx);

CodePudding user response:

The Model class actually has asMap() method. You can check in java docs . Context class in turn has context.setVariables() method. You can check it here. It means you can write something like this:

ctx.setVariables(model.asMap());

It is also worth noting that you should use the Model class and the Context class as they have different responsibilities. Since Model class is used to add attributes to a model that will be used to render a view, and the Context class is used to add attributes to a context that will be used to process a Thymeleaf template.

  • Related