springboot-web、Thymelea

目录

一、springboot中的静态资源

        1、在springboot中,我们可以使用以下方式处理静态资源

        2、定制springboot首页

二、Thymeleaf模板引擎 

        1、导入jar包

        2、Thyleleaf的使用 

        3、Thyleleaf的基础语法

                  1、text和untext

                   2、each

一、springboot中的静态资源

        1、在springboot中,我们可以使用以下方式处理静态资源

                a、webjars: 使用maven导入静态资源的坐标 localhost:8080/webjars/...(可以直接访问下面的静态资源)

                b、把静态资源放在roureous目录下的 public,static,/**,resources(自己创建的)         localhost:8080/...

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

                c、在配置文件中主动配置静态资源的路径:spring.mvc.static-path-pattern=....

        2、定制springboot首页

                可以直接在静态资源的目录下编写index.html      项目启动后默认进入index.html首页

                网页图标设置:在resources下的static下面放入名为favicon.ico

二、Thymeleaf模板引擎 

        前端交给我们的页面,是html页面。如果是我们以前开发,我们需要把他们转成jsp页面,jsp好处就是当我们查出一些数据转发到JSP页面以后,我们可以用jsp轻松实现数据的显示,及交互等。jsp支持非常强大的功能,包括能写Java代码,但是呢,我们现在的这种情况,SpringBoot这个项目首先是以jar的方式,不是war,像第二,我们用的还是嵌入式的Tomcat,所以呢,他现在默认是不支持jsp的。

        模板引擎有很多,Springboot推荐使用Thymeleaf

        Thymeleaf的使用:

        1、导入jar包

	    <dependency>
			<groupId>org.thymeleaf</groupId>
			<artifactId>thymeleaf-spring5</artifactId>
		</dependency>
		<dependency>
			<groupId>org.thymeleaf.extras</groupId>
			<artifactId>thymeleaf-extras-java8time</artifactId>
		</dependency>

        2、Thyleleaf的使用 

             使用Thleleaf要在HTML页面加上命名空间:xmlns:th="http://www.thymeleaf.org"

             使用Thyleleaf模板引擎必须将要跳转的HTML页面放入resources下的templates目录下

                controller

@Controller
public class HelloController {
    @RequestMapping("/test")
    public String test1(Model model){
        model.addAttribute("msg","rk学Springboot");
        return "test";
    }
}

                test.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
        <title>Title</title>
</head>
<body>
<h1 th:text="${msg}"></h1>
</body>
</html>

                结果

        3、Thyleleaf的基础语法

                  1、text和untext

  model.addAttribute("msg","<h1>rk学Springboot</h1>");
<div th:text="${msg}"></div>
<div th:utext="${msg}"></div>

 

                   2、each

   model.addAttribute("users", Arrays.asList("罗林","王敏"));
<div th:each="user:${users}" th:text="${user}"></div>

   

        

                

                

       

                

猜你喜欢

转载自blog.csdn.net/m0_46979453/article/details/120901530