简介
使用SpringBoot;
1)、创建SpringBoot应用,选中我们需要的模块;
2)、SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来
3)、自己编写业务代码;
自动配置原理?
这个场景SpringBoot帮我们配置了什么?能不能修改?能修改哪些配置?能不能扩展?xxx
xxxxAutoConfiguration:帮我们给容器中自动配置组件;
xxxxProperties:配置类来封装配置文件的内容;
SpringBoot对静态资源的映射规则;
以前给项目打war
包的时候,前端的文件css,js
,图片的等静待资源都是放在webapp
下的
但是现在springboot
使用的打包是为jar
的形式,这种方式springboot
能不能给我们写页面?
这也是可以的。只是页面,css要放哪,springboot有规定。
1.,方式一
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties implements ResourceLoaderAware {
//可以设置和静态资源有关的参数,缓存时间等
WebMvcAutoConfiguration
:
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();
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));
}
}
所有/webjars/**
,都去classpath:/META-INF/resources/webjars/
找资源
webjars
:以jar包的方式引入静态资源
https://www.webjars.org/
到webjars
官网找一个jquery:
<!--jquery-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
启动项目,试一下能不能访问:
http://localhost:8081/webjars/jquery/3.3.1/jquery.js
2),方式二
/**
:访问当前项目的任何资源 (静态资源的文件夹)
都是静态资源文件夹
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/**"
localhost:8080/abc
如果没人处理,默认去静态资源文件夹里面找abc
。
往static
文件夹放静态资源,访问时可以不加static
:
http://localhost:8081/asserts/js/bootstrap.min.js
3)、欢迎页; 静态资源文件夹下的所有index.html
页面;被"/**
"映射;
往静态资源文件夹下放一个index.xml
localhost:8080/ 会自动去静态资源文件夹下找index
页面
4),spring.resources.static-locations=
可以修改默认的静态资源文件路径
模板引擎
springboot推荐的页面模板是thymeleaf在前后端不分离的情况下,springboot推荐用html做页面,然后用thymeleaf做模板引擎,做数据渲染,但是这种方式还是要用js或者jquery手动去操作dom,很难受,前后端分离的情况下直接使用vue、react等
如果前端交给我们的页面是html
,如果是以前要转成jsp
页面,但是现在springboot
使用的是jar
包的方式启动并且还是嵌入式的tomcat
是不支持jsp
的,如果我们使用纯静态的页面的方式,会给我们开发带来非常大的麻烦。springboot
推荐使用模板引擎。
常用的模板引擎:
JSP、Velocity、Freemarker、Thymeleaf
springboot推荐Thymeleaf;语法更简单,功能更强大;
1.引入Thymeleaf
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.Thymeleaf
使用
@ConfigurationProperties(prefix = "spring.thymeleaf")
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";
只要我们把HTML
页面放在classpath:/templates/
,thymeleaf
就能自动渲染;
测试:http://localhost:8081/success
@RequestMapping("/success")
public String success(){
//classpath:/templates/success.html
return "success";
}
classpath:/templates/`下success.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>陈宫!!</h1>
</body>
</html>
thymeleaf官网:下载文档
https://www.thymeleaf.org/documentation.html
使用:
1.导入thymeleaf
的名称空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
2、使用thymeleaf
语法
@RequestMapping("/success")
public String success(Map<String,Object> map){
//classpath:/templates/success.html
map.put("hello","你好" );
return "success";
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>陈宫!!</h1>
<!--th:text 将div里面的文本内容设置为-->
<div th:text="${hello}">这是显示欢迎信息</div>
</body>
</html>
3、语法规则
1)、
th:text;改变当前元素里面的文本内容;
th:任意html属性;来替换原生属性的值