1

I'd like to be able to configure different json templates based on e.g. a clientId with a default template in case a template for the clientId is not defined.

    Context context = new Context();
    
    Map<String, Object> templateResolutionAttribute = new HashMap<>();
    templateResolutionAttribute.put("clientId", 12345);//Trying to somehow pass a variable to the template selection process...        

    TemplateSpec templateSpec = new TemplateSpec(
            "json/data.json",
            templateResolutionAttribute
    );

    String output = springTemplateEngine.process(templateSpec, context);

The Spring Config class I'm using looks like this:

@Configuration
public class ThymeLeafConfig {
    @Bean
    public SpringResourceTemplateResolver jsonMessageTemplateResolver() {
        SpringResourceTemplateResolver theResourceTemplateResolver =
                new SpringResourceTemplateResolver();
        theResourceTemplateResolver.setPrefix("classpath:/templates/");
        theResourceTemplateResolver.setResolvablePatterns(
                Collections.singleton("json/*"));
        theResourceTemplateResolver.setSuffix(".json");
        theResourceTemplateResolver.setCharacterEncoding("UTF-8");
        theResourceTemplateResolver.setCacheable(false);
        theResourceTemplateResolver.setOrder(1);
        return theResourceTemplateResolver;
    }

The json templates could be in different directories or alternatively have different file names (whatever works best/easiest):

json/
  data.json
  12345/
    data.json

or

json/
  data.json
  data.12345.json

What is the best or easiest way to achieve returning a template that "matches" the clientId and if no match for the clientId is found return a default template?

Note: For new clients, ideally, I'd like to be able to just add new json templates without making any code changes...

Bernie Lenz
  • 1,967
  • 23
  • 45
  • Maybe I am misunderstanding the question - but it does not really appear to have anything _specifically_ to do with Thymeleaf. All you need to do is check if a resource exists or not. You can build the relevant path and file name using your ID (e.g. `12345`) and then test whether it actually exists - see [Spring: Check if a classpath resource exists before loading](https://stackoverflow.com/q/53931046/12567365). If it exists, use it. Otherwise, use the default. – andrewJames Feb 15 '23 at 23:04
  • "_...without making any code changes..._" - If the directories are not on the classpath of the app (e.g. not bundled in `/resources/templates`), then it's just a simple [test to see if the file exists](https://stackoverflow.com/q/1816673/12567365) or not. In that case, then yes, you would need to adjust your Thymeleaf config `setPrefix("classpath:/templates/")`. – andrewJames Feb 15 '23 at 23:06
  • @andrewJames I was hoping that Thymeleaf would have a dynamic selection of some sort built in, especially since it accepts a templateResolutionAttribute Map. However templateResolutionAttribute doesn't seem to be used anywhere which is weird... – Bernie Lenz Feb 16 '23 at 02:42
  • 2
    Ah - I take your point. I don't know how a map of `templateResolutionAttribute` values affects the behavior of the template resolver. Even [this discussion](https://github.com/thymeleaf/thymeleaf/issues/423) of that feature did not make it clear (to me, anyway). Maybe it will to you. – andrewJames Feb 16 '23 at 12:55
  • 1
    The only thing I can see is that "_template resolution attributes are considered a part of the identifier of a template, so they will be used as a part of the keys for cached templates_" [ref](https://www.thymeleaf.org/apidocs/thymeleaf/3.0.11.RELEASE/org/thymeleaf/TemplateSpec.html#%3Cinit%3E(java.lang.String,java.util.Map)). So, for cached template retrieval, I can see they play a role. – andrewJames Feb 16 '23 at 12:59
  • 1
    Yeah, it really looks like the templateResolutionAttributes are not used when picking up files. I'm surprised though that not more people asked for a feature like that. To me templating goes hand in hand with being able to configure different templates based on attributes... – Bernie Lenz Feb 16 '23 at 14:06

0 Answers0