相比于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分段表达式:~{...}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)