使用springboot:
- 创建springboot应用,选择需要的场景;
- springbooty已经默认将这些场景配置好了,只需在配置文件中指定少量配置就可以运行起来;
- 自己编写业务逻辑代码;
1.springboot对静态资源的映射规则
@ConfigurationProperties(prefix="spring.resources",ignoreUnknownFileds=false) public class ResourceProperties implements ResourceLoaderAware{ //可以设置和静态资源有关的参数,缓存时间等
1.所有/Webjars/**,都去classpath:/META-INF/resources/webjars/找资源;
webjars:以jar包的方式引入静态资源;
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使用文档第四章)
|
4.SpringMVC自动配置
http://www.spring-boot.org/doc/pages/spring-boot-features.html#boot-features-json-json-b 第28节
Thymeleaf