How to do it...

To use Thymeleaf as the templating procedure for rendering reactive contents, follow these steps:

  1. Before this recipe starts, be sure to have the Spring Reactive dependency included in pom.xml since we are building now a reactive web application.
  2. If the rendition requires the use of non-blocking Mono<T> and Flux<T> operations, then Thymeleaf is the appropriate templating library to use, because FreeMarker cannot directly recognize non-blocking operations. To integrate Thymeleaf for Spring 5, add the following Maven dependencies:
<dependency> 
    <groupId>org.thymeleaf</groupId> 
    <artifactId>thymeleaf-spring5</artifactId> 
    <version>3.0.6.M4</version> 
</dependency> 
  1. Open the SpringWebReactiveConfig context definition and inject the following Thymeleaf configuration details. Also, inject the application's ApplicationContext which is needed by SpringResourceTemplateResolver:
@Autowired 
private ApplicationContext applicationContext; 
 
@Bean(name ="templateResolver")   
public SpringResourceTemplateResolver getTemplateResolver() { 
      SpringResourceTemplateResolver templateResolver =  
new SpringResourceTemplateResolver(); 
      templateResolver.setApplicationContext( 
applicationContext); 
      templateResolver.setPrefix("/WEB-INF/templates/"); 
      templateResolver.setSuffix(".html"); 
      templateResolver.setTemplateMode("XHTML"); 
      return templateResolver; 
} 
    
@Bean(name ="templateEngine")      
public SpringTemplateEngine getTemplateEngine() { 
    SpringTemplateEngine templateEngine =  
new SpringTemplateEngine(); 
    templateEngine.setTemplateResolver( 
getTemplateResolver()); 
    return templateEngine; 
} 
     
@Bean(name="viewResolverThymeLeaf") 
public ThymeleafViewResolver getViewResolverThyme(){ 
    ThymeleafViewResolver viewResolver =  
new ThymeleafViewResolver();  
    viewResolver.setTemplateEngine(getTemplateEngine()); 
    viewResolver.setOrder(2); 
    return viewResolver; 
} 
  1. Open the RenderController and add the following request handler that will render a Flux<Employee> to a Thymeleaf template:
@RequestMapping(value="/thymeleaf/empList.html",  
   method=RequestMethod.GET) 
public String users(Model model){ 
   model.addAttribute("employees", 
       new ReactiveLazyContextVariable( 
employeeServiceImpl.readEmployeesByDescAge())); 
   return "thyme_list_emps"; 
} 
Extension of Thymeleaf templates is preferred to be .html.
  1. Create a Thymeleaf template in WEB-INF emplatesxxxx.html that will serve as the physical view of the request handler above:
<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
 <head> 
    <title>Ch08 Thymeleaf Reactive View</title> 
 </head> 
 <body> 
    <table> 
      <thead> 
        <tr> 
          <th>Employee ID</th> 
          <th>First Name</th> 
          <th>Last Name</th> 
          <th>Age</th> 
        </tr> 
      </thead> 
      <tbody> 
        <tr th:each="e : ${employees}"> 
          <td th:text="${e.empId}"></td> 
          <td th:text="${e.firstName}"></td> 
          <td th:text="${e.lastName}"></td> 
          <td th:text="${e.age}"></td> 
        </tr> 
      </tbody> 
    </table> 
 </body> 
</html> 

  1. Save all files. Then clean, build, and deploy the project. Run the URL in any browser and expect the same output as the previous recipe.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.97.53