SpringBoot学习(三)之 Web开发

SpringBoot对静态资源的映射规则

  1. https://www.webjars.org/
    所有/webjars/** 都回去classpath"/META-INF/resources/webjars/找资源
    webjars:以jar包的方式引入静态资源
		<dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.3.1</version>
        </dependency>

在这里插入图片描述
访问链接:

http://localhost:8080/webjars/jquery/3.0.0/jquery.js
  1. “/**” 访问当前项目的任何资源(静态资源的文件夹)
    在这里插入图片描述
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public
“/”:当前项目的根路径


localhost:8080/hello.html
会自动在上面的路径下找对应的静态资源。

  1. 首页 index.html
  2. 配置图标
    **/favicon.ico 都是在静态资源文件下查找
    html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="icon" type="image/x-icon" href="/resources/favicon.ico">
</head>
<body>
<h1>hello world</h1>
</body>
</html>

在这里插入图片描述

具体源码:WebMvcAutoConfiguration

@Bean
			public SimpleUrlHandlerMapping faviconHandlerMapping() {
				SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
				mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
				mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
						faviconRequestHandler()));
				return mapping;
			}

自定义静态文件夹,不推荐,因为原来的会作废。

spring.resources.static-location=classpath:/hello,classpath:/world

1.引入thymeleaf

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

目前我使用的springboot使用了3.0.11 thymeleaf 最新版。
如果后续需要更新版本,可以在pom.xmk文件中配置信息
更新新的版本号,覆盖Springboot原有的版本号:

<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-extras-data-attribute.version>2.0.1</thymeleaf-extras-data-attribute.version>

2.语法使用

文档路径
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.pdf
需要把html页面放到classpath:/templates, thymeleaf就能自动渲染。
1.导入thymeleaf的名称空间

<html xmlns:th="http://www.thymeleaf.org">

2.使用语法:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值