Is there a way to programmatically handle missing/misspelled template parameters in Thymeleaf?
I've read How to throw exception is template variable not found in Context?. The answer proposes adding validation inside the template. I already have a lot of templates, so changing them would be a lot of work. Plus: If someone fets/typos a variable, they are just as likely is fet adding the validation as well.
To be more specific: I have a class like this
class TemplateRenderer {
private final TemplateEngine templateEngine;
public TemplateRenderer() {
this.templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(new StringTemplateResolver());
}
public String render(String template, Map<String, Object> variables) {
IContext context = new Context(Locale.GERMAN, variables);
return templateEngine.process(template, context);
}
}
That is used like this:
public class Main {
public static void main(String[] args) {
TemplateRenderer renderer = new TemplateRenderer();
System.out.println(renderer.render("<span th:text=${key} />", Map.of("key", "value")));
System.out.println(renderer.render("<span th:text=${misspelled_key} />", Map.of("key", "value")));
System.out.println(renderer.render("<span th:text=${key} />", Map.of("misspelled_key", "value")));
System.out.println(renderer.render("<span th:text=${key} />", Map.of()));
}
}
The first println outputs <span >value</span>
as expected, the last three output <span ></span>
.
Is there a way I can cause the last three to log a warning or throw an exception? Is there some custom hook I can implement inside my TemplateRenderer class?
Is there a way to programmatically handle missing/misspelled template parameters in Thymeleaf?
I've read How to throw exception is template variable not found in Context?. The answer proposes adding validation inside the template. I already have a lot of templates, so changing them would be a lot of work. Plus: If someone fets/typos a variable, they are just as likely is fet adding the validation as well.
To be more specific: I have a class like this
class TemplateRenderer {
private final TemplateEngine templateEngine;
public TemplateRenderer() {
this.templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(new StringTemplateResolver());
}
public String render(String template, Map<String, Object> variables) {
IContext context = new Context(Locale.GERMAN, variables);
return templateEngine.process(template, context);
}
}
That is used like this:
public class Main {
public static void main(String[] args) {
TemplateRenderer renderer = new TemplateRenderer();
System.out.println(renderer.render("<span th:text=${key} />", Map.of("key", "value")));
System.out.println(renderer.render("<span th:text=${misspelled_key} />", Map.of("key", "value")));
System.out.println(renderer.render("<span th:text=${key} />", Map.of("misspelled_key", "value")));
System.out.println(renderer.render("<span th:text=${key} />", Map.of()));
}
}
The first println outputs <span >value</span>
as expected, the last three output <span ></span>
.
Is there a way I can cause the last three to log a warning or throw an exception? Is there some custom hook I can implement inside my TemplateRenderer class?
Share Improve this question asked Feb 14 at 14:00 SörenSören 2,4564 gold badges24 silver badges37 bronze badges1 Answer
Reset to default 0I'm guessing you could do something like
public String render(String template, Map<String, Object> variables) {
final Set<String> badVariables = new LinkedHashSet<>();
IContext context = new IContext() {
public boolean containsVariable(String name) {
boolean result = variables.containsKey(name);
if (!result) badVariables.add(name);
return result;
}
public Object getVariable(String name) {
Object result = variables.get(name);
if (result == null && !variables.containsKey(name)) {
badVariables.add(name);
}
return result;
}
public Set<String> getVariableNames() {
return variables.keySet();
}
public Locale getLocale() {
return Locale.GERMAN;
}
};
String result = templateEngine.process(template, context);
if (!badVariables.isEmpty()) {
throw new RuntimeException("Bad variables " + badVariables);
}
return result;
}