SpringBoot框架(四)

目录

导入静态资源

方式一:使用webjars

方式二

首页的添加方式

 页面的图标定制

2.2版本以前的方式

2.2版本以后的方式

 Thymeleaf

举例:th:text、th:utext、th:each的应用


导入静态资源

查看WebMvcAutoConfiguration的源码

     public void addResourceHandlers(ResourceHandlerRegistry registry) {
			//如果静态资源路径自定义了,则直接返回
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
            //使用webjars导入静态资源
                this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
                this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
                    registration.addResourceLocations(this.resourceProperties.getStaticLocations());
                    if (this.servletContext != null) {
                        ServletContextResource resource = new ServletContextResource(this.servletContext, "/");
                        registration.addResourceLocations(new Resource[]{resource});
                    }

                });
            }
        }

不使用webjars的方式,导入静态资源(继续读源码)

进入WebProperties,可以看出静态资源存放的位置一共有4个

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

进入WebMvcProperties,访问路径为/**

 private String staticPathPattern = "/**";

导入静态资源的方式如下:

方式一:使用webjars

webjars:实则就是将静态资源封装成jar,再通过maven坐标的方式进行导入

先导入webjars的相关坐标

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.4.1</version>
        </dependency>

查看资源路径

 访问的路径为:localhost:8080/webjars/jquery/3.4.1/jquery.js

该/webjars/ 实则为jar包中/META-INF/resources/webjars/

方式二

优先级:resources>static(默认)>public

访问路径为:http://localhost:8080/test.js 

首页的添加方式

在public、resources、static、根目录(resources)、templates里创建index.html,一般存入templates中,放进templates里需要使用模板引擎

 页面的图标定制

2.2版本以前的方式

  • 将图片(.ico)放入static中,命名为favicon.ico
  • 在主配置文件中添加配置
spring:
  mvc:
    favicon:
      enabled: false
  • 测试即可

2.2版本以后的方式

上述的自定义方式无法使用,所以我们需要在页面中调用使用

  • 将图片(.ico)放入static中,命名为favicon.ico
  • 在index.html中设置
    <link rel="icon" href="/favicon.ico">

运行结果

 Thymeleaf

Thymeleaf是一款java模板引擎,实现了界面和数据的分离,大大提高了开发效率。(JSP也是java模板引擎)

添加maven坐标

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

 查看ThymeleafProperties的源码

   private String prefix = "classpath:/templates/";
   private String suffix = ".html";

该模板已经设置了前缀、后缀,例如转发至index.html,只需写成return "index"即可,(这个功能类似于springmvc的视图解析器)

控制器代码

@Controller
public class testCollector {
    @GetMapping("index.html")
    public String index(){
        return "index";
    }
}

 使用Thymeleaf,html一定要导入约束

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

Thymeleaf基本语法

推荐网站:

Thymeleaf3语法详解和实战 - ITDragon龙 - 博客园 (cnblogs.com)

Thymeleaf 教程 - Thymeleaf | Docs4dev

举例:th:text、th:utext、th:each的应用

控制层:

@Controller
public class testCollector {
    @GetMapping("index.html")
    public String index(Model model){
//        传入string、集合两种数据
        model.addAttribute("text","<h1>text</h1>");
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        model.addAttribute("lists",arrayList);
        return "index";
    }
}

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="icon" href="/favicon.ico">
</head>
<body>
首页
<hr>
<!--不解析特殊字符-->
<p th:text="${text}"></p>
<!--解析特殊字符-->
<p th:utext="${text}"></p>
<!--遍历集合-->
<h3 th:each="list:${lists}" th:text="${list}"></h3>
</body>
</html>

页面效果

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值