1.Spring Boot mapping rules for static resources

If you’re not familiar with SpringBoot static pages, take a look at my Thymeleaf post 1:

This is Thymeleaf links: aaatao66. Making. IO / 2019/05/26 /…

  • If you have index. HTML in the static resources folder,index is the welcome page if you go to localhost:8080

Use your own icon:

Put a Favicon. ico icon in the static resources folder and springBoot will automatically call this as our icon

I found a tinker Bell icon

Effect:

Custom static resource folders

Under your yML/Properties add:

spring.resources.static-locations=classpath:/hello/,classpath:/carson/
Copy the code

Once defined, the original default cannot be used

2. Introduce the Thymeleaf

SpringBoot recommends thymeleaf because of its simple syntax and power. JSP is not recommended and is not supported by default

 <! Thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
Copy the code

New versions of Spring Boot will automatically set the version, but if you are 1.x Spring Boot, you may need to change the version yourself

3. Thymeleaf uses &

Source:

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    
    // The suffix below is the prefix, which means it can be rendered as long as it is placed in the prefix directory
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
Copy the code
  1. Write a controller method:
  @RequestMapping("nice")
    public String nice(a){
        return "nice";
    }
Copy the code
  1. Create nice.html in the Templates directory under Resources

      
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>This is the NICE tag</h1>
</body>
</html>
Copy the code
  1. Start the program with localhost:8080/nice

Use:

1. Import the namespace of Thymeleaf into the HTML page

<html lang="en" xmlns:th="http://www.thymeleaf.org">
Copy the code

2. Use thymeleaf syntax

  • controller:
 @RequestMapping("nice")
    public String nice(Map<String,Object> map){
        map.put("hello"."Hello");
        return "nice";
    }
Copy the code
  • nice.html

      
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>This is the NICE tag</h1>
	<! Get the text content of the scope
<div th:text="${hello}"></div>
</body>
</html>
Copy the code

Grammar rules

1.th:text; Change the contents of the current element.

Th: Any HTML attribute to replace the native attribute

  • HTML code:
<div id="div01" class="mydiv" th:id="${hello}" th:class="${hello}" th:text="${hello}"></div>
Copy the code
  • Spring Boot after parsing the source code:

      
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>This is the NICE tag</h1>

<div id="Hello" class="Hello">hello</div>
</body>
</html>
Copy the code

Both id and class have been replaced

Attribute priority

expression

Specific reference: official documents

Simple expressions () Variable expressions: ${... } : get the variable value; 2. Use the built-in base object # CTX: the Context object. # Vars: The Context variables. the context locale. #request : (only in Web Contexts) the HttpServletRequest object. #response : (only in Web Contexts) the HttpServletResponse object. #session : (only in Web Contexts) the HttpSession object. #servletContext : (only in Web Contexts) the ServletContext object Selection Variable Expressions: *{... ${$} is the same as ${$}. } Link URL: @{... } Define URL Fragment Expressions: ~{... } Literals Text Literals: 'One Text', 'Another one! ',... Number literals: 0, 34, 3.0, 12.3... Boolean literals: true, false Null literal: Null literal tokens: one, someText, main... Text operations: (Text operation String concatenation: + Literal substitutions: | The name is ${name} | Arithmetic operations: Binary operators: +, -, *, /, % Minus sign (unary operator): -boolean operators: and , or Boolean negation (unary operator): ! Word order: >, <= (gt, lt, GE, le) Equality operators: ==,! = (eq, ne) Conditional operators: (Conditional operation) if-then: (If)? (then) If-then-else: (if) ? (then) : (else) Default: (value) ? : (defaultvalue) Special tokens: Page 17 of 104 No-Operation: _Copy the code

The tests are as follows:

  • The controller code:
@RequestMapping("nice")
    public String nice(Map<String,Object> map){
        map.put("hello"."< h1 > hello < h1 / >");
        map.put("users", Arrays.asList("zhangsan"."lisi"."wangwu"));
        return "nice";
    }
Copy the code
  • The HTML code

      
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>This is the NICE tag</h1>

<div id="div01" class="mydiv" th:id="${hello}" th:class="${hello}" th:text="${hello}"></div>
<hr/>
<div th:text="${hello}"></div>
<div th:utext="${hello}"></div>
<hr/>
<! -- th:each iteration generates the current tag -->
<h4 th:text="${user}" th:each="user:${users}"></h4>
<hr/>
<h4>
    <span th:each="user:${users}">[[${user}]]</span>
</h4>
</body>
</html>
Copy the code
  • The command output is as follows:

  • Web source code:


      
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>This is the NICE tag</h1>

<div id="< h1> Hello & lt; h1/>" class="< h1> Hello & lt; h1/>">&lt; h1&gt; Hello & lt; h1/&gt;</div>
<hr/>
<div>&lt; h1&gt; Hello & lt; h1/&gt;</div>
<div><h1>hello<h1/></div>
<hr/>
<! -- th:each iteration generates the current tag -->
<h4>zhangsan</h4>
<h4>lisi</h4>
<h4>wangwu</h4>
<hr/>
<h4>
    <span>zhangsan</span><span>lisi</span><span>wangwu</span>
</h4>
</body>
</html>
Copy the code

Summary:

<div th:text="${hello}"></div>
<div th:utext="${hello}"></div>
Copy the code

Th :text: Escape special characters so the page shows

Hello

Th: UText: Does not escape special characters, the page displays the H1 tag header hello

Th :each: writes on the H4 tag, and each traversal produces a new H4 tag:

zhangsan

lisi

wangwu

zhangsan lisi wangwu


My nuggets: juejin.cn/user/536217…

Personal blog: aaatao66.github. IO /

Welcome attention praise ~~ I will continue to record their own learning records ~