Thymeleaf + spring dynamic replace

后端 未结 5 1808
花落未央
花落未央 2021-02-06 00:47

Is it possible to create a dynamic replace in Thymeleaf?

I have the following controller:

@Controller
public class LoginController {

    @RequestMapping         


        
5条回答
  •  灰色年华
    2021-02-06 01:12

    I believe the appropriate method to manage this behavior in thymeleaf is to use layout:fragment tags. Please correct me if I'm wrong. Here is a simple example of my layout page, and the login page which is 'dynamically' loaded:

    layout.html

    
    
        Layout
        
        
    
    
    

    Then, when login gets loaded, it replaces the th:fragment div with the associated div in the html view which matches the string returned by the controller method, in this case login.html:

    
    
        Login
    
    
    

    Now, if you want to load another fragment conditionally, the approach I take is to add replace tags with th:if cases. Here's an example of a Form that displays different questions based on an attribute of the current user:

    Then the associated div gets loaded from the file custom-questions.html:

    //stuff
    //stuff

提交回复
热议问题