SpringBoot2.x入门到项目实战课程系列(第五章)( 三 )
- 总结:
- 当接受到 /** 请求访问资源时, 会被映射到下面4个类路径下的静态资源目录中查找
classpath:/META-INF/resources/classpath:/resources/classpath:/static/classpath:/public/- 访问 localhost:8080/style.css 会在上面四个静态资源路径 中查找文件
- 在 WebMvcAuotConfiguration.welcomePageHandlerMapping() 分析 欢迎页映射
@Beanpublic WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) { return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext=======查找欢迎页========, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());}- getWelcomePage() 方法获取 欢迎页面 可存储路径
private Optional getWelcomePage() { String[] locations = ===2. 上面说的4个静态资源路径加上 "/" 路径 getResourceLocations( =====1. 获取上面说的4个静态资源路径 this.resourceProperties.getStaticLocations()); ==================================在上面路径下查找 index.html 页面 return Arrays.stream(locations).map(this::getIndexHtml). filter(this::isReadable).findFirst();}// 上面获取的路径中查找 index.html 页面private Resource getIndexHtml(String location) { return this.resourceLoader.getResource(location + "index.html");} - 分析后, 会从 4个静态资源目录 + 根路径 / 中 查找 index.html 页面
classpath:/META-INF/resources/classpath:/resources/classpath:/static/classpath:/public//: 当前项目根路径下- 会在 静态资源目录下 与 根路径查找 (按该顺序) index.html页面; 收到 “/**” 请求映射
- 访问 localhost:8080/ 会在上面5个目录中查找 index.html 页面(因为/也属于 /** )
- Spring Boot 会在静态资源目录下 与 根路径(按该顺序) 查找 faicon.ico 页面;
- 如果存在这样的文件 , Spring Boot 会自动将其设置为应用图标 。
classpath:/META-INF/resources/classpath:/resources/classpath:/static/classpath:/public//: 当前项目根路径下5.3 Thymeleaf 模板引擎Spring Boot 官方不推荐使用JSP , 因为内嵌的 Tomcat 、Jetty 容器不支持以 jar 形式运行 JSP 。 Spring Boot中提供了大量模板引擎 , 包含 Freemarker、Mastache、Thymeleaf 等 。而 Spring Boot 官方推荐使用Thymeleaf 作为模板引擎 ,因为 Thymeleaf 提供了完美的 SpringMVC 的支持 。
文章插图5.3.1 引入 Thymeleaf
- pom.xml 加入 Thymeleaf 启动器
org.springframework.boot spring-boot-starter-thymeleaf 5.3.2 使用 Thymeleaf- 模板文件放在哪里 ?
@ConfigurationProperties( prefix = "spring.thymeleaf" )public class ThymeleafProperties { private static final Charset DEFAULT_ENCODING; public static final String DEFAULT_PREFIX = "classpath:/classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html";- 通过上面分析发现 ,将 HTML 页面放到 classpath:/templates/ 目录下 ,Thymeleaf 就能自动渲染
推荐阅读
- 徐福记联手JDL京东物流向数智化转型,首次落地智慧园区项目
- 苹果两款新iPad齐曝光:性能提高、入门款更轻薄、售价便宜
- 运动计数开发项目的对抗赛:飞算全自动软件工程平台碾压传统模式
- RHEL 9提升了x86_64处理器的入门要求
- 江北新区企业院士工作站技术攻关项目立项数位居南京第一
- 入门HiFi享好声,这几款耳机绝对值得入手
- 赞!盐城高新区4个项目入选省级项目立项!
- 威海高新区2项目获2020年度山东省重点研发计划(重大科技创新工程)立项支持
- 浙江公布首批“互联网+”医疗服务价格项目!适用各级公立医疗机构
- 168天封顶!投资56亿重点AI项目将在上海竣工,提供世界领先算力支撑
