相比于springmvc-web开发要解决的问题:
-
导入静态资源
-
指定首页
-
jsp。模版引擎Thymeleaf
-
装配扩展SpringMVC
-
增删改查
-
截器和国际化
静态资源
idea按两下shift,搜索webautoConfiguration - WebMvcAutoConfigurationAdapter - addResourceHandlers
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
// 第一种方式 webjars
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
// 第二种方式
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
-
第一个if:没有
-
第二个if:webjars不推荐使用,官网导包,url:META-INF/resources/webjars/
-
第三个if:优先级:resources > static(默认) > public
-
classPath :/**
-
classpath:/META-INF/resources/
-
classpath:/resources/
-
classpath:/static/(默认创建)
-
classpath:/public/
在pom.xml中导入jquery https://www.webjars.org/
<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>jquery</artifactId>
<version>3.6.0</version>
</dependency>
![]()
Thymeleaf
- 注意:
template/**下的任何页面都需要Controller来跳转才能访问,不能直接访问
<!--thymeleaf模版引擎在,都是基于3.x版本开发-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
第一次使用,传msg,接受:th:text="${msg}",(thymeleaf独有的语法)
//在templates目录下的所有页面,只能通过Controller来跳转
//这个需要模板引擎的支持
@Controller
public class IndexController {
@RequestMapping("/text")
public String index(Model model){
model.addAttribute("msg","<h1>烟雨如花</h1>");
//Arrays.asList可以把一个数组转化成一个集合
model.addAttribute("users", Arrays.asList("烟雨如花","赤色夫人"));
return "text";
}
}
<!DOCTYPE html>
<!--thymeleaf约束-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>thymeleaf</title>
</head>
<body>
<!--所有的html元素都可以被Th接管:th:xx元素名-->
<!--test=默认是转义文本-->
<h1 th:text="${msg}"></h1>
<!--utest=默认是不转义文本-->
<h1 th:utext="${msg}"></h1>
<h1>
<!--遍历往前写item-->
遍历一 推荐这么使用:
<h2 th:each="user:${users}" th:text="${user}"></h2><br>
遍历二:
<h2 th:each="user:${users}" >[[${user}]]</h2>
</h1>
</body>
</html>
简单的表达式:
Varlable Expressions:(变量表达式)${…}
Selection Varlable Expressions选择变量表达式:*{…}
Message Expressions消息表达式:# {...}
Link URL Expressions链接URL表达式:@{...}
Fragment Expressions分段表达式:~{...}
简单的表达式:
Varlable Expressions:(变量表达式)${…}
Selection Varlable Expressions选择变量表达式:*{…}
Message Expressions消息表达式:# {...}
Link URL Expressions链接URL表达式:@{...}
Fragment Expressions分段表达式:~{...}