Git Product home page Git Product logo

spring-thymeleaf-example's Introduction

Thymeleaf, a worthy alternative

If you were looking for a decent alternative for those old JSPs, look no further.

Thymeleaf is a Java library. It is an XML / XHTML / HTML5 template engine (extensible to other formats) that can work both in web and non-web environments. It is better suited for serving XHTML/HTML5 at the view layer of web applications, but it can process any XML file even in offline environments.

How does Thymeleaf compare to other web frameworks?

From their web page FAQs

Thymeleaf makes a strong stress on natural templating —allowing templates to be working prototypes and its syntax tries to be cleaner and more in tune with the current trends in web development. Also, from an architectural standpoint, both Velocity and FreeMarker work as sequential text processors —which allows them to process many types of content— whereas Thymeleaf is based on XML parsing techniques —which limits it to XML-based formats. This makes Velocity and FreeMarker much more versatile, but on the other side allows Thymeleaf to take advantage of interesting features specific to XML-based environments, especially the web.

About this blog post

In this tutorial, I'll briefly show how we can serve a web page using spring boot and thymeleaf. This blogpost won't go into any depth of how thymeleaf works, or how you can use it. Instead, it will teach you how you can set up your environment with spring boot to serve the pages.

Accompanying code

This small tutorial is accompanied by a Github Repository. Not all content of the code at the repository will be discussed here, so don't forget to check it out later!

Just a basic Controller

The controller we'll be using will be kept simple, as this is a tutorial on how to setup a thymeleaf application with spring boot, not an entire blogpost on spring mvc.

@Controller
@RequestMapping(value = "/")
public class IndexController {
    @RequestMapping(method = RequestMethod.GET)
    public String index(ModelMap map) {
        return "main";
    }
}

This will map the root of context to the index() method. Because the index returns "main", the template engine will try to resolve the page in your application with the name "main".

Booting the application

The class which bootstraps the entire application is ThymeLeafExample. It will automatically configure everything for you, based on your gradle imports, which is discussed below.

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class ThymeLeafExample {
	public static void main(String[] args) throws Exception {
    	SpringApplication.run(ThymeLeafExample.class);
	}
}

Gradle import

For the sake of brevity, below you'll see the gradle imports that are necessary to use thymeleaf in a Spring Boot environment.

compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")

While starting up, Spring Boot will automatically detect the presence of the starter-web and starter-thymeleaf modules, and therefore making sure the correct interceptors and template engines are started.

Your resources

Resources are defined - as the name implies - in your resources folder. Thymeleaf will automatically search for pages in your templates folder, directly under resources. In our controller, we returned "main" as index page. /resources/templates/main.html will be automatically resolved.

Our Desired Result

If you run the example, as pulled from github using the following command:

gradle bootRun

and navigate to http://localhost:8080 you should get something along the lines of the following screenshot.

Coming up

In one of my future blogposts, I'll be discussing some more in depth on how we can change some of the default settings for thymeleaf.

spring-thymeleaf-example's People

Contributors

qkyrie avatar n0ha avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.