SpringBoot(二)web开发部分

1. SpringBoot静态资源的映射规则

可以设置和静态资源有关的参数,缓存时间等

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {

WebMvcAutoConfiguration.java源码

		/静态资源文件夹映射:
		@Override
		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));
			}
		/配置欢迎页映射:
		@Bean
		public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
				FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
			WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
					new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
					this.mvcProperties.getStaticPathPattern());
			welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
			welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
			return welcomePageHandlerMapping;
		}
		};
		
		<!‐‐引入jquery‐webjar‐‐>在访问的时候只需要写webjars下面资源的名称即可
		<dependency>
            <groupId>org.webjars.bower</groupId>
            <artifactId>jquery</artifactId>
            <version>3.3.1</version>
        </dependency>

1)所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;
webjars:以jar包的方式引入静态资源;
http://www.webjars.org/
https://mvnrepository.com/

通过localhost:8080/webjars/jquery/3.3.1/dist/jquery.js访问js
在这里插入图片描述
2)"/**" 访问当前项目的任何资源,都去下面五个(静态资源的文件夹)找映射

"classpath:/META‐INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径

localhost:8080/abc == 去静态资源文件夹里面找abc

http://localhost:8080/asserts/js/Chart.min.js
在这里插入图片描述
3)欢迎页; 静态资源文件夹下的所有index.html页面;被"/**"映射;
在这里插入图片描述
http://localhost:8080/ 找index页面

4)所有的 **/favicon.ico 都是在静态资源文件下找;
在这里插入图片描述
在这里插入图片描述
5)自定义静态文件夹路径
在这里插入图片描述

2. 模板引擎

模板引擎可以让你实现代码分离(业务逻辑代码和用户界面代码),也可以实现数据分离(动态数据与静态数据),还可以实现代码单元共享(代码重用),甚至是多语言、动态页面与静态页面自动均衡(SDE)等等与用户界面可能没有关系的功能

JSP、Velocity、Freemarker、Thymeleaf

在这里插入图片描述

2.1 引入thymeleaf

springboot2.x.x以上版本 自动匹配

	<!-- 布局功能的支持程序  thymeleaf3主程序  layout2以上版本 -->
	<!-- thymeleaf2   layout1-->
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

在这里插入图片描述

2.2 Thymeleaf使用&语法

只要我们把HTML页面放在classpath:/templates/,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";

1、导入thymeleaf的名称空间

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

2、使用thymeleaf语法;

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
        <h2>成功</h2>
    <div th:text="${hello}">这是显示欢迎信息</div>
</body>
</html>

在这里插入图片描述
3、语法规则
1)、th:text;改变当前元素里面的文本内容;
th:任意html属性;来替换原生属性的值
在这里插入图片描述
2)、表达式?(查看官方文档)

@Controller
public class HelloController {

    //查出用户数据,在页面展示
    @RequestMapping("/success")
    public String success(Map<String,Object> map){
        map.put("hello","<h1>你好</h1>");
        map.put("users",Arrays.asList("zhangsan","lisi","wangwu"));
        return "success";
    }
}
<body>
        <h2>成功</h2>
    <div id="div01" class="myDiv" th:id="${hello}" th:class="${hello}" th:text="${hello}">这是显示欢迎信息</div>
<hr>
	<div th:text="${hello}"></div>
	<div th:utext="${hello}"></div>
<hr>
	<h4 th:text="${user}" th:each="user:${users}"></h4>
<hr>
	<h4>
    	<span th:each="user:${users}">[[${user}]]</span>
	</h4>
</body>

在这里插入图片描述

3. SpringMVC自动配置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值