Basic Spring MVC example with Thymeleaf and no XML (annotation based configuration)

Since the version 3.x Springframework allows to write applications without any use of xml. It’s quite funny the idea that Spring the ‘xml oriented framework’ now is completely xml-free.

I tried to develop a website (a simple page to be honest) using the annotation configuration and the new ‘kid on the block’: Thymeleaf.

I think I’ll develop these example to build a new real website.

As usual you can find the source on github:

We created a class that contains the Thymeleaf configuration : templateResolver, templateEngine, viewResolver

public class ThymeleafConfig { 
    public TemplateResolver templateResolver(){ 
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); 
        return templateResolver; 
    public SpringTemplateEngine templateEngine(){ 
        SpringTemplateEngine templateEngine = new SpringTemplateEngine(); 
        return templateEngine; 
    public ViewResolver viewResolver(){ 
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver() ; 
        return viewResolver; 

You have to declare a ViewResolver that allow Spring to find and create the answer page. In the case of a ‘traditional’ jsp the ViewResolver would be the following:

//Defines the ViewResolver that Spring will use to render the views. 
    public ViewResolver viewResolver() { 
        System.out.println("Inside View Resolver..."); 
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
        return viewResolver; 

The AppConfiguration class is the main class of the application. The @EnableWebMvc tells to Spring that we are developing a web application.

@Import it’s the equivalent of ‘‘ in XML.
It allows us to use the beans declared in other configuration files.

As you can see the AppConfiguration can be empty.

@ComponentScan(basePackages = "ch.javaee.simpleMvc") 
@Import({ WebInitializer.class, DispatcherConfig.class}) 
public class AppConfiguration { 

The WebInitializer implements the WebApplicationInitializer that configures the ServletContext.

You can find more information in the Spring official documentation.

In brief, this class is bootstrapped by the application server (Servlet v.3.0) and it registers the dispatcher.

public class WebInitializer implements WebApplicationInitializer { 
    public void onStartup(ServletContext container) throws ServletException { 
        // Create the 'root' Spring application context 
        AnnotationConfigWebApplicationContext rootContext = 
                new AnnotationConfigWebApplicationContext(); 
        // Manage the lifecycle of the root application context 
        container.addListener(new ContextLoaderListener(rootContext)); 
        // Create the dispatcher servlet's Spring application context 
        AnnotationConfigWebApplicationContext dispatcherContext = 
                new AnnotationConfigWebApplicationContext(); 
        // Register and map the dispatcher servlet 
        ServletRegistration.Dynamic dispatcher = 
                container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); 


There is no need of any xml file. We don’t use any web.xml or any configuration.xml file. Spring allow to combine the use of xml files with @Configuration annotations simplifying the integration of legacy code.

The integration of Thymeleaf is straightforward. As declared in the templateResolver bean the web pages are found in the /WEB-INF/templates/ directory.

The @Controller assign the value “Hello world!” to the attribute “title” and pass it to the “main” page.

@RequestMapping(method = RequestMethod.GET) 
	public String printWelcome(ModelMap model) { 
		model.addAttribute("title", "Hello world!"); 
		return "main"; 

Eventually, the “main.html” page receives and shows the value of the attribute.

<img src="../../images/jt_net.png" th:attr="src=@{images/jt_net.png}"/> 
    <h2 th:text="${title}">Title</h2> 

Here you can see the result:


Super fast WebApp built by Marco using SpringBoot 3 and Java 17 hosted by Infomaniak in Switzerland