In my Spring MVC project I use Tiles to eliminate any redundant page structure code. As a view renderer, I first chose Thymeleaf because it was described as a promising new kid on the block and pretty easy to use. Unfortuantely, the integration of Thymeleaf with Tiles depends on deprecated Spring (Tiles2) classes, furthermore the only Thymeleaf forum is almost inactive and questions mostly remain unanswered.
Therefore I want to switch to Freemarker because it has a well established user community and is also supported by Spring. But the configuration of the integrated Spring MVC, Tiles3 and Freemarker is apparently not documented and most examples on the internet are using Tiles in combination of JSP or Freemarker without Tiles, but I cannot find a clear, working integrated example.
I am not very experienced with Spring MVC and configurations, but I think the following issues must be addressed:
-Loading the Tiles definition file. According to some examples, it can be done using following method in the MvcConfig class:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "nl.drsklaus.activiteitensite.web")
public class MvcConfig extends WebMvcConfigurerAdapter {
...other methods
@Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer tc = new TilesConfigurer();
tc.setDefinitions(new String[] {"/WEB-INF/views/tiles/defs/tiles-defs.xml"});
return tc;
}
}
-Resolving the returning String from a controller method to a Tiles definition:
This should be done using following method in the MvcConfig class:
@Bean
public ViewResolver tilesViewResolver() {
TilesViewResolver viewResolver = new TilesViewResolver();
viewResolver.setViewClass(TilesView.class);
// viewResolver.setOrder(1);
return viewResolver;
}
My first question is whether this Tiles view resolver should be in some way "Freemarker-aware". According to the Tiles documentation, the attribute 'type="freemarker"' should be added to the tag as well as to the tags in the Tiles definition XML file, but is it unclear to me what is the effect of that. Furthermore, the fragments as referenced in the value attribute of those put-attribute tags must be parsed as Freemarker templates instead of JSP. I do not see how this should be configured.
In fact it is unclear to me how the lookup/resolving proces from a Tiles fragment in Spring works. Is the extension .ftl anough for Spring to know that is should look for a Freemarker template?
Some examples lists code fragments like these:
@Bean
public FreeMarkerConfigurer freemarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/views/freemarker");
configurer.setDefaultEncoding("utf-8");
return configurer;
}
Is this method enough for Spring MVC to load the Freemarker templates from the Tiles definition? How does Spring "know" that a template referenced by Tiles is actually a Freemarker template? There also exists a FreeMarkerViewResolver class which extends UrlBasedViewResolver. But is is unclear to me whether and how we should use this class.
I hope for some clarification and maybe even an integration example :)