• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

WebContext in Thymeleaf

November 22, 2013 //  by Krishna Srinivasan//  Leave a Comment

In my previous tutorial I have explained about writing your first Hello World example using Thymeleaf template engine framework. If you look at the example, it uses some kind of context to transfer the variables across its application. The same way how ServletContext or SpringApplicationContext works, Thymeleaf has its own context framework to maintain its context across the applications and retain the values throughout the application scope. This tutorial explains the various types of context available and default values stored with the thymeleaf’s context environment.

There are two context available with this template engine

  1. org.thymeleaf.context.Context implements IContext
  2. org.thymeleaf.context.WebContext implements IWebContext

The second one WebContext is our preferred contact object to use in our application. This context is specific to Web Applications. This itself extending the IContext. We are not directly using the IContext in our web applications, it contains very common behavior of the context mechanism, this may be used in the future releases when introducing some more sub context apart from the WebContext.

You can create a WebContext like this:

[code lang=”java”] WebContext ctx = new WebContext(request, servletContext, request.getLocale());
[/code]

When the context is created, it creates a object which holds the two values for the template engine. The object name is execInfo. The two variables are templateName and now. these can be accessed across anywhere in the templates.

[code] ${execInfo.templateName}
${execInfo.now}
[/code]

As told, this context object used for transferring the values to template engine. You can set the values to context object as you do with normal JSP or Servlet.

[code lang=”java”] WebContext ctx = new WebContext(request, servletContext, request.getLocale());
ctx.setVariable("today", dateFormat.format(cal.getTime()));
[/code]

Category: Web FrameworksTag: Thymeleaf

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « java.lang.ClassNotFoundException: javassist.ClassPath
Next Post: Top 5 Best Free Online CSS3 Tools For Web Design »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact