Spring Boot
views
default directory
templates
MVC

By default, where does Spring Boot expect views to be stored?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Spring Boot is a widely used framework simplifying the setup and development of new Spring applications. It defaults many configurations, marking it a go-to for developers who value convention over configuration. Among these conventions is the expected location for view templates, which are critical in web applications for rendering HTML content based on data fetched during request handling.


Where Does Spring Boot Expect Views to Be Stored?

In a standard Spring Boot application, the framework expects view templates to be stored within specific directories inside the src/main/resources directory, depending on the templating engine you are using (e.g., Thymeleaf, FreeMarker, Mustache, etc.). Understanding these defaults is key for smooth starting phases in web application development.

Default View Locations:

  1. Thymeleaf Views:
    • Directory: src/main/resources/templates
    • Extensions: .html When using Thymeleaf as your template engine, Spring Boot looks for template files with an .html extension inside the templates directory. These templates are then rendered and returned as a response to the client.
  2. FreeMarker Views:
    • Directory: src/main/resources/templates
    • Extensions: .ftl Similar to Thymeleaf, FreeMarker templates are placed in the templates directory. The different file extension (.ftl) indicates that these files are parsed and rendered by the FreeMarker engine.
  3. Mustache Views:
    • Directory: src/main/resources/templates
    • Extensions: .mustache Mustache template files also reside within the templates directory, helping to minimize configuration changes when switching between templating languages.
  4. JSP Views:
    • Directory: src/main/webapp/WEB-INF/jsp
    • Extensions: .jsp Note that using JSPs requires a traditional servlet container such as Apache Tomcat and proper packaging as a WAR file since JSPs are processed on the server-side.

Here's a summarized table of these defaults:

Template EngineDefault DirectoryFile Extension
Thymeleafsrc/main/resources/templates.html
FreeMarkersrc/main/resources/templates.ftl
Mustachesrc/main/resources/templates.mustache
JSPsrc/main/webapp/WEB-INF/jsp.jsp

Customizing the View Location

Spring Boot offers flexibility allowing developers to change the default view directory path if necessary. This can be done by setting properties in the application.properties or application.yml file.

For example, to change the location of Thymeleaf templates, you can set the following property:

In application.properties:

  • Complex project structures.
  • Multi-module projects with shared resources.
  • Requirement for dynamic template resolution.

Course illustration
Course illustration

All Rights Reserved.