Spring入门(五)——web开发

简介

使用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属性;来替换原生属性的值
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值