Skip to content

juliuskrah/thymeleaf-fragments-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Thymeleaf Fragments Example

Build Status

This is to illustrate layouts example in thymeleaf using the Thymeleaf Standard Layout System. For more information on using the standard template check out this article.

The template used in this example was downloaded from Start Boostrap.

This example uses Thymeleaf 3.0.0.RELEASE to render the templates.

Some minor configuration changes are required to use Thymeleaf 3 with Spring 4.

Pre-requisites

  • Maven 3.3+
  • Java 8+
  • Jetty 9+ or Tomcat 8+ (Optional)
  • Spring Framework 4.2.x
  • Thymeleaf 3.0.x

Configuration

The fist thing required is to declare your depencies in your pom.xml

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
  <version>3.0.0.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf-spring4</artifactId>
  <version>3.0.0.RELEASE</version>
</dependency>

The second thing to do is your spring configuration

@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = { Controllers.class })
public class WebAppConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
	private ApplicationContext applicationContext;

	private static final String VIEWS = "classpath:templates/";
	...
	
	@Bean
	public ViewResolver viewResolver() {
		ThymeleafViewResolver resolver = new ThymeleafViewResolver();
		resolver.setTemplateEngine(templateEngine());
		resolver.setCharacterEncoding("UTF-8");
		return resolver;
	}

	private TemplateEngine templateEngine() {
		SpringTemplateEngine engine = new SpringTemplateEngine();
		engine.setTemplateResolver(templateResolver());
		return engine;
	}

	private ITemplateResolver templateResolver() {
		SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
		resolver.setApplicationContext(applicationContext);
		resolver.setPrefix(VIEWS);
		resolver.setSuffix(".html");
		resolver.setTemplateMode(TemplateMode.HTML);
		return resolver;
	}
}

The first difference with the Thymeleaf 3 configuration is that now the recommended template resolver for Spring applications is SpringResourceTemplateResolver. It needs a reference to the Spring ApplicationContext so the configuration bean has to implement the ApplicationContextAware interface.

The second difference is that the template mode has a value of TemplateMode.HTML. Template modes are not strings anymore and the possible values are a bit different from Thymeleaf 2.

Running the Application

Your can run the application by specifying a maven goal mvn jetty:run, which will start an embedded Jetty instance. Visit your new application on http://127.0.0.1:8080/.

About

Using Thymeleaf 3 with fragments

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages