5, View parsing and template engine

5, View parsing and template engine

  • SpringBoot does not support jsp by default, so it is necessary to introduce third-party template engine technology to realize page rendering
1. Introduction to Thymeleaf
  • Modern, server-side Java template engine

  • Use in SpringBoot

    • Introducing a starter (scenario launcher)

              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-thymeleaf</artifactId>
              </dependency>
      
    • The prefix and suffix have been specified in SpringBoot under the temlates folder

    • To introduce a noun space into an html file:

      xmlns:th="http://www.thymeleaf.org"
      
2. Expression
Expression namegrammardescribe
Variable value${xxx}Get data in request domain, Session domain and object
Select variable*{xxx}Get context object value
news#{xxx}Get international equivalent
link@{xxx}Generate link
Fragment Expression~{xxx}It is equivalent to jsp:include, which introduces public page fragments
3. Literal quantity
  • Use single quotation marks for text values
  • Null is used for null values
4. Text operation
  • String splicing+
  • Variable replace The name is ${name}
5. Boolean operation
  • Operators: and or
  • Unary operation:, not
6. Comparison operator
  • >, <, > =, < = (GT, lt, Ge, le * *) * equation: = == ( eq , ne )
7. Iteration
<tr th:each="prod : ${prods}">
        <td th:text="${prod.name}">Onions</td>
        <td th:text="${prod.price}">2.41</td>
        <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
8. Judge
  • th:if

    <a href="comments.html"
    th:href="@{/product/comments(prodId=${prod.id})}"
    th:if="${not #lists.isEmpty(prod.comments)}">view</a>
    
  • th:switch
    <div th:switch="${user.role}">
      <p th:case="'admin'">User is an administrator</p>
      <p th:case="#{roles.manager}">User is a manager</p>
      <p th:case="*">User is some other thing</p>
    </div>
    
9. Inline writing
  • [[${session.loginUser.userName}]]
  • Use [[]] to get rid of the shackles of the label
10. Extract public parts
  • <body>
      <div th:insert="footer :: copy"></div>
    
      <div th:replace="footer :: copy"></div>
    
      <div th:include="footer :: copy"></div>
    </body>
    
  • <body>
      <div>
        <footer>
          &copy; 2011 The Good Thymes Virtual Grocery
        </footer>
      </div>
      <footer>
        &copy; 2011 The Good Thymes Virtual Grocery
      </footer>
      <div>
        &copy; 2011 The Good Thymes Virtual Grocery
      </div>
    </body>
    
11. View analysis principle
  1. In the process of target method processing, all data will be placed in ModelAndViewContainer, including data and view address
  2. The parameter of the method is a custom type object (determined from the request parameter), and put it back in the ModelAndViewContainer
  3. Modelandview (data and view address) is returned after the execution of any target method
  4. processDispatchResult handles the dispatch result (how the page responds)
    1. render(mv,request,response): perform page rendering logic
      1. Return the value to the View object according to the String of the method [defines the rendering logic of the page]
        • All input parsers try to see if they can get to the View object based on the current return value
        • Get redirect: / main -- > thymeleaf new redirectview()
        • Content negotiation viewresolver quinoa contains all of the following parsers. Internally, it still uses all of the following views to parse the view objects
        • view.render(mv,getModelInternal(),request,response): the view object calls the custom render to render the page
          • RedirectView: how to render [redirect to a page]
            1. Get destination url address
            2. response.sendRedirect(encodedURL)
  • View resolution:

    • The return value starts with forward: new internalresourceview (forwardurl) - > forward

      request.getRequestDispatcher(path).forward(request,response)

    • The return value starts with redirect: new redirectview() -- > render is redirect

    • The return value is a normal string: new ThymeleafView()

Tags: Java Spring Boot

Posted on Fri, 12 Nov 2021 01:33:59 -0500 by denniston