In my previous articles I have explained about the hello world example and the expression language. This tutorial explains how to use the switch statements inside Thymeleaf template. Thymeleaf provides th:switch attribute for using switch functionality. This feature is extension of my previous example on iteration in thymeleaf template.
1. Set Up Thymeleaf Application
To demonstrate this example, I am using our previous hello world example.Use the same example and replace our code snippet as below. You can use the set up details as mentioned in the hello world example.
2. Create Template with th:switch
If you look at the below example, the tamplate has the th:switch which is replaced with the dynamic values by the template engine.
employees.xml
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <title>Iteration in Thymeleaf Template Engine!!</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h1>Employees</h1> <table> <tr> <th>SNO</th> <th>ID</th> <th>NAME</th> <th>CITY</th> <th>COUNTRY</th> <th>ROLE</th> </tr> <tr th:each="emp,iterationStatus : ${employees}"> <td th:text="${iterationStatus.count}">1</td> <td th:text="${emp.id}">001</td> <td th:text="${emp.name}">Name</td> <td th:text="${emp.city}">City</td> <td th:text="${emp.country}">Country</td> <td> <div th:switch="${emp.role}"> <p th:case="'Admin'">User is an Administrator</p> <p th:case="'Manager'">User is a Manager</p> <p th:case="'Engineer'">User is an Engineer</p> </div> </td> </tr> </table> </body> </html>
3. Create Bean Employee
Employee.java
package javabeat.net.thymeleaf; public class Employee { private String id; private String name; private String role; private String city; private String country; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } }
4. Create Servlet with Thymeleaf Context and Attach Template
ThymeleafSwitchExample.java
package javabeat.net.thymeleaf; import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; import java.util.TreeSet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.WebContext; import org.thymeleaf.templateresolver.ServletContextTemplateResolver; public class ThymeleafSwitchExample extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); // XHTML is the default mode, but we will set it anyway for better understanding of code templateResolver.setTemplateMode("XHTML"); templateResolver.setPrefix("/WEB-INF/"); templateResolver.setSuffix(".html"); templateResolver.setCacheTTLMs(3600000L); TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); WebContext ctx = new WebContext(req, resp, getServletConfig().getServletContext(), req.getLocale()); ArrayList<Employee> employees = new ArrayList<Employee>(); Employee employee = new Employee(); employee.setId("001"); employee.setName("Rahul"); employee.setRole("Manager"); employee.setCity("Bangalore"); employee.setCountry("India"); employees.add(employee); employee = new Employee(); employee.setId("002"); employee.setName("Deepan"); employee.setRole("Admin"); employee.setCity("Namakkal"); employee.setCountry("India"); employees.add(employee); employee = new Employee(); employee.setId("003"); employee.setName("Ashwanth"); employee.setRole("Engineer"); employee.setCity("Bangalore"); employee.setCountry("India"); employees.add(employee); // This will be prefixed with /WEB-INF/ and suffixed with .html ctx.setVariable("employees", employees); templateEngine.process("employees", ctx, resp.getWriter()); resp.setContentType("text/html;charset=UTF-8"); resp.setHeader("Pragma", "no-cache"); resp.setHeader("Cache-Control", "no-cache"); resp.setDateHeader("Expires", 1000); } }
5. Run The Thymeleaf Application
If you execute the above code, you would see the below output as below.