SpringBoot对静态资源的访问
如果我们需要给web项目中添加css/js/html文件的话,我们会发现此时没有webapp目录。
springboot创建的项目没有保存web资源的位置
如果我们需要给springboot项目中添加web资源如果操作?
我们要了解一个Java类"WebMvcAuotConfiguration",因为于web开发相关的自动配置都是由这个类完成的。
public void addResourceHandlers(ResourceHandlerRegistry registry) {
.......
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
........
}
@EnableConfigurationProperties({WebMvcProperties.class, WebProperties.class})
WebProperties.java
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[] {"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"};
1.静态资源的保存位置所有"/webjars/**"
webjars:将需要使用的静态资源打成jar包,我们如果需要就将整个har导入至本项目就可以使用了
例如:以JQuery为例来使用一下webjars方式
1.https://www.webjars.org/---静态资源打成jar包查看位置
2.在pom.xml文件中导入JQuery的jar依赖
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.5.1</version>
</dependency>
2.测试访问我们导入的jquery文件
启动服务,在浏览器地址栏中直接访问jquery文件
http://localhost:8080/webjars/jquery/3.5.1/jquery.js
3.默认静态资源文件的位置
@EnableConfigurationProperties({WebMvcProperties.class, WebProperties.class})
WebProperties.java
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[] {"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"};
1."classpath:/META-INF/resources/"===[src/main/resources/META-INF/resources]
运行之前需要打包一下
http://localhost:8080/test.html
2.classpath:/resources/===[src/main/resources/resources]
http://localhost:8080/test1.html
3.classpath:/static/===[src/main/resources/static]
http://localhost:8080/test2.html
4.classpath:/public/===[src/main/resources/public]
http://localhost:8080/test3.html
模板引擎
由于springboot是以jar包的方式打包程序的,而不是web项目,再者在springboot中,我们用的是嵌入式的tomcat服务器,因此springboot项目是不支持动态页面的运行,所以我们都将页面处理成静态页面的话,那么到时候加载数据的时候就需要大量的js来向后台请求数据回填到静态页面上,这样的话就会比较麻烦,所以springboot才为我们提供了模板引擎。
常见的模板引擎由JSP、Velocity、Freemarker、Thymeleaf
SpringBoot为我们推荐Thymeleaf模板引擎;因为他语法更简单,功能更强大。
Thymeleaf模板引擎的使用:
1.引入Thymeleaf模板引擎的依赖包【启动器】
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.在src/main/resources/templates/,及那个html模板创建在templates下,thymeleaf就能自动渲染
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
<li><h2 th:text="${id}"></h2></li>
<li><h2 th:text="${name}"></h2></li>
<li><h2 th:text="${age}"></h2></li>
<li><h2 th:text="${address}"></h2></li>
</ul>
<!--xmlns:th="http://www.thymeleaf.org"----在html文件中启用thymeleaf的标-->
</body>
</html>
3.创建Controller孔子其为html模板提供数据
package com.springboot.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
public class TestController {
@RequestMapping( "test1")
public String testHello(Map<String,Object> map){
map.put("id",1001);
map.put("name","张三");
map.put("age",23);
map.put("address","西安");
return "ModelYinQing";
}
@RequestMapping(value = "/test2")
public String testHello2(Model model){
model.addAttribute("id",1001);
model.addAttribute("name","张三");
model.addAttribute("age",23);
model.addAttribute("address","西安");
return "mytest";
}
}
注意:1.请求处理方法的参数,是给模板包装数据
【1.Map<String,Object> map 2.Model model】
2.请求处理放啊的返回值,是html模板的名称,不用带".html"的后缀