要解决的第一个问题,静态资源存放问题,静态资源放在哪儿能查找到。html
首先查看WebMvcAutoConfiguration.class(能够直接全局查找)前端
protected void addResourceHandlers(ResourceHandlerRegistry registry) { //是否有自定义配置,有的话这个方法失效 super.addResourceHandlers(registry); if (!this.resourceProperties.isAddMappings()) { logger.debug("Default resource handling disabled"); } else { //添加这个webjars,添加以后路径为:classpath:/META-INF/resources/webjars/ ServletContext servletContext = this.getServletContext(); this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/"); this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> { //其他能够识别的静态资源位置 registration.addResourceLocations(this.resourceProperties.getStaticLocations()); if (servletContext != null) { registration.addResourceLocations(new Resource[]{new ServletContextResource(servletContext, "/")}); } }); } }
那么,什么是webjars?java
WebJars是一个很神奇的东西,可让你们以jar包的形式来使用前端的各类框架、组件。WebJars是将客户端(浏览器)资源(JavaScript,Css等)打成jar包文件,以对资源进行统一依赖管理。jquery
个人理解就是:以 依赖的形式导入前端资源git
结合上面的路径,咱们导入一个jqury
尝试:web
<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.5.1</version> </dependency>
能够看出 一一对应,也能够正常访问。spring
除了上面这些,还有其余能够识别静态资源的位置:浏览器
//其他能够识别的静态资源位置 registration.addResourceLocations(this.resourceProperties.getStaticLocations());//getStaticLocations() 点击 if (servletContext != null) { registration.addResourceLocations(new Resource[]{new ServletContextResource(servletContext, "/")}); } public String[] getStaticLocations() { return this.staticLocations; //staticLocations 点击 } private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"}; // 出现了路径
至于 templates文件夹,其中的文件只能用 Controller 控制类,才能够进入。springboot
在SpringBoot中,默认自动识别在静态资源文件夹下的 index.htmlmvc
注意:
- 静态资源文件夹为:
- public
- static
- resources
- 优先级:resources > static > public
- 默认的首页文件为:index.html,不要弄错。
下面测试放在了public,其他方法你们能够自行测试:
模板引擎有什么用呢? 可让你使用 Controller类 调用页面,没有Thymeleaf就是不能够。
首先!!,最重要的是导入这两个依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.4.3</version> </dependency>
缘由:本身建立的SpingBoor项目,自带的Thymeleaf版本为2.xxx,而咱们要使用的这个版本为3.xx
你们导入依赖后肯定是否成功:
以后更改一下pom.xml中的配置:
<properties> <java.version>1.8</java.version> <!-- 加入下面这两句 --> <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>3.0.4</thymeleaf-layout-dialect.version> </properties>
建立一个Controller类
注意:必定要在 xxxApplication启动器的同级目录下建立,否则不可能成功!!(踩了30分钟的坑)
@Controller public class ThymeleafController { @RequestMapping("/a") public String test(){ return "tt"; } }
建立一个html页面
注意:SpringBoot内部规定要在templates文件夹下建立xxx.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>ThymeleafController跳转</h1> </body> </html>
以后运行测试:
th:text
修改controller类,使用model传一个数据:
@RequestMapping("/a") public String test(Model model){ model.addAttribute("msg","Thymeleaf语法测试"); return "tt"; }
修改html:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>ThymeleafController跳转</h1><br> <!--以往都是直接${msg},在Thymeleaf中须要如下使用方法:--> <!--以前html能够用的标签,都变为: th:属性名 --> <div th:text="${msg}"></div> </body> </html>
th:utext
修改controller类,使用model传一个数据:
@RequestMapping("/a") public String test(Model model){ model.addAttribute("msg","<h2>Thymeleaf语法测试</h2>"); return "tt"; }
修改html:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>ThymeleafController跳转</h1><br> <div th:text="${msg}"></div><br> <div th:utext="${msg}"></div><br> </body> </html>
th:each
修改controller类,使用model传一个数据:
@RequestMapping("/a") public String test(Model model){ model.addAttribute("users", Arrays.asList("user1","user2","user3")); return "tt"; }
修改html:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>ThymeleafController跳转</h1><br> <!-- 两种写法,推荐第一种 --> <h3 th:each="user:${users}" th:text="${user}"></h3> <div th:each="user:${users}" >[[ ${user} ]]</div> </body> </html>
diy视图解析器
@Configuration public class MyMvcConfig { @Bean public ViewResolver MyViewResolver(){ return new MyViewResolver(); } public static class MyViewResolver implements ViewResolver{ @Override public View resolveViewName(String s, Locale locale) throws Exception { return null; } } }
@Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { // 这句话的意思就是访问 ...8080/test, 跳转到tt.html页面 registry.addViewController("/test").setViewName("tt"); } }
在SpringBoot中,有很是多的 xxxConfiguration 帮助咱们进行扩展配置,只要看见了这个东西,咱们就要注意了,多是增长了一些新的功能。
我的博客为:
MoYu's HomePage
MoYu's Gitee Blog