一、导入静态资源
1.1 静态资源优先级
可以直接访问:localhost:8080/1.js
二、首页
方式一:
直接在static目录中添加index.html即可
方式二:需要先学下一个模板引擎
templates中的html页面,但是只能通过Controller访问
图标管理:
1、springboot:2.1.7
2、application.properties
# 关闭默认图标
spring.mvc.favtion.enabled=false
3、将自己的图标导入项目,放在resources/public下面即可,命名为:favtion.ico
4、然后清除浏览器缓存
三、模板引擎Thymeleaf
1、导入依赖
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
2、编写html
在templates目录中写html。然后再Controller中写跳转链接即可
源码: 前缀-后缀
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
// 前缀
public static final String DEFAULT_PREFIX = "classpath:/templates/";
// 后缀
public static final String DEFAULT_SUFFIX = ".html";
3、thymeleaf的语法
3.1、添加 命名空间
<html lang="en" xmlns:th="http://www/thymeleaf.org">
官方文档:
3.2、取消息,类比Vue
<h1 th:text="${msg}"></h1>
3.3、常用语法
text 、utext:转义
// 传参数:<h1>hello,springboot</h1>
<h1 th:text="${msg}"></h1> <!--<h1>hello SpringBoot~xd</h1>-->
<h1 th:utext="${msg}"></h1> <!--hello SpringBoot~xd-->
each:遍历
<!--model.addAttribute("users", Arrays.asList("xd","xd1","xd2"));-->
<h3 th:each="user:${users}" th:text="${user}"></h3>
<h3 th:each="user:${users}" >[[${user}]]</h3>
其余的看源码或者官方文档
四、装配扩展SpringMVC
当我们需要扩展SpringMvc的时候,只用实现就完事了!
1、自定义视图跳转
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//自定义视图跳转
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/xd").setViewName("test");
}
}