Spring Boot学习记录(五、WEB开发)

使用springboot:

  1. 创建springboot应用,选择需要的场景;
  2. springbooty已经默认将这些场景配置好了,只需在配置文件中指定少量配置就可以运行起来;
  3. 自己编写业务逻辑代码;

1.springboot对静态资源的映射规则

@ConfigurationProperties(prefix="spring.resources",ignoreUnknownFileds=false)
public class ResourceProperties implements ResourceLoaderAware{
//可以设置和静态资源有关的参数,缓存时间等

1.所有/Webjars/**,都去classpath:/META-INF/resources/webjars/找资源;

webjars:以jar包的方式引入静态资源;

http://www.webjars.org/

localhost:8080/webjars/jquery/3.3.1/jquery.js

<!-- 引入jQuery-webjar -->
<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.4.1</version> </dependency>

2. “/**”访问当前项目的任何资源,(静态资源的文件夹)

"classpath://META-INF/resources/",
"classpath://resources/",
"classpath://static/",
"classpath://public/",
"/":当前项目的根路径

localhost:8080/xxx  (去静态文件夹里面找xxx)

3.欢迎页:静态资源文件夹下的所有index.html页面;被"/**"映射;

localhost:8080/  找index页面

4.所有的 **/favicon.ico 都是在静态资源文件下找;

2.模板引擎

例如:jsp、 Velocity、Freemaker、Thymeleaf。

springboot推荐使用的Thymeleaf语法更简单,功能更强大;

1.引入Thymeleaf

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2.Thymeleaf使用&语法

Thymeleaf使用文档:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

默认前缀=“classpath:/templates/”  默认后缀=“.html”

    @RequestMapping("/success")
    public String success(){
        return "success";
    }

1.html页面中导入Thymeleaf的名称空间

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

2.使用Thymeleaf语法

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>成功!~ lol</h1>
<p th:text="${hello}"/>
</body>
</html>

3.语法规则

    1) th:text  改变当前元素的文本内容

        th:任意html属性  来替换原生属性的值

    2)  表达式(Thymeleaf使用文档第四章)

  • 简单表达:
    • 变量表达式:${...}
    • 选择变量表达式:*{...}
    • 讯息表达:#{...}
    • 链接URL表达式:@{...}
    • 片段表达:~{...}
  • 文字
    • 文本文字:'one text''Another one!',…
    • 数字文字:0343.012.3,…
    • 布尔文字:truefalse
    • NULL文字:null
    • 文字标记:onesometextmain,…
  • 文本操作:
    • 字符串级联:+
    • 字面替换:|The name is ${name}|
  • 算术运算:
    • 二进制运算符:+-*/%
    • 减号(一元运算符):-
  • 布尔运算:
    • 二进制运算符:andor
    • 布尔否定(一元运算符):!not
  • 比较和平等:
    • 比较国:><>=<= (gtltgele)
    • 平等操作员:==!= (eqne)
  • 条件运算符:
    • 如果-然后:(if) ? (then)
    • 如果-然后-否则:(if) ? (then) : (else)
    • 违约:(value) ?: (defaultvalue)
  • 特殊令牌:
    • 不-行动:_

4.SpringMVC自动配置

http://www.spring-boot.org/doc/pages/spring-boot-features.html#boot-features-json-json-b 第28节

Thymeleaf

猜你喜欢

转载自www.cnblogs.com/nirvanaInSilence/p/12446178.html