- 在resources创建包来实现,
- 一、springboot对静态资源的映射规则
- 这些自动装配先在pom导依赖,然后springboot的autoconfiguration就自动配置,在外部资源中找xxxproperties就是该资源的自动配置文件
- 有css html文件夹
- springboot对静态资源也做了映射的规则 :实现了资源的 加载
- 做了一些整合
- 1.所有的/webjars/**,都去classpath:/META-INF/resouces/webjars/找资源,所以在写静态页面的时候要在resource下面创建
- 2./**访问当前项目的任何资源,都去(静态资源的文件夹)找映射!
- 默认都在这些,目录下找
- 默认都在这些,目录下找
- 3.欢迎页:静态资源文件夹下的所有index.html页面,被/**映射
- localhost:8080/index.html 在static下面写这个页面
- 二、模板引擎thymeleaf
- 模板引擎就是你呈现数据的那个页面
- 数据从数据库来,这个整个过程是:查询出来之后,controller调业务层,业务层调用持久层,持久层从数据库中拿,通过model往前带到界面上之后,通过视图引擎来解析这个数据,单纯的html不能解读特殊的标签,jsp可以解读这些特殊的标签,因为jsp的真身是servlet
- jsp和视图引擎是绑在一起的,thymeleaf是借助html才有的视图引擎,需要靠视图引擎解析一些特殊的字符然后呈现
- spring是什么版本 thymeleaf就是什么版本
- 大部分的依赖根据父工程的版本来决定自身的版本
- 我们使用html作为模板,而且默认的前缀是放在classpath:/templates/下,后缀是.html,当然这些属性可以通过application.properties来修
- 流程:引入thymeleaf的流程
- 1.在template下创建一个success.html
- 2.在html中引入thymeleaf
- 3.创建一个controller提供一个访问的方法
- 4.在thymeleaf模板中取值 text是文本text='${hello}' $里面的内容来自于controller
- 三、thymeleaf的语法
- 1.变量表达式:<span th:text="${book.author.name}">在spring术语中也叫做modelattributes${session.user.name}
- 2.选择表达式:用一个预先选择的对象来代替上下文变量容器*{customer.name}
- 3.文字国际化表达式:可以从一个外部文件获取区域文字信息(.properties),用key索引value,还可以提供一组参数#{main.title}
- 4.url表达式:把一个有用的上下文或者会话信息添加到url,这个过程经常被叫做url重写不需要指定项目名字@{/order/list}
- 四、常用的thymeleaf标签
- 关键字 功能介绍 案例
- th:id 替换id <input th:id="'xxx' + ${collect.id}"/>
- th:text 文本替换 <p th:text="${collect.description}">description</p>
- th:utext 支持html的文本替换 <p th:utext="${htmlcontent}">conten</p>
- th:object 替换对象 <div th:object="${session.user}">
- th:value 属性赋值 <input th:value="${user.name}" />
- th:onclick 点击事件 th:onclick="'getCollect()'"
- th:each 属性赋值 tr th:each="user,userStat:${users}">
- th:if 判断条件 <a th:if="${userId == collect.userId}" >
- th:unless 和th:if判断相反 <a th:href="@{/login}" rel="external nofollow" rel="external nofollow" rel="external nofollow" th:unless=${session.user != null}>Login</a>
- th:href 链接地址 <a th:href="@{/login}" rel="external nofollow" rel="external nofollow" rel="external nofollow" th:unless=${session.user != null}>Login</a> />
- th:switch 多路选择 配合th:case 使用 <div th:switch="${user.role}">
- th:case th:switch的一个分支 <p th:case="'admin'">User is an administrator</p>
- th:fragment 布局标签,定义一个代码片段,方便其它地方引用 <div th:fragment="alert">
- th:include 布局标签,替换内容到引入的文件 <head th:include="layout :: htmlhead" th:with="title='xx'"></head> />
- th:replace 布局标签,替换整个标签到引入的文件 <div th:replace="fragments/header :: title"></div>
- th:selected selected选择框 选中 th:selected="(${xxx.id} == ${configObj.dd})"
- th:src 图片类地址引入 <img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" />
- th:action 表单提交的地址 <form action="subscribe.html" th:action="@{/subscribe}">
- jar包:执行SpringBoot主类的main方法,启动ioc容器,创建嵌入式的Servlet容器;
- war包:启动服务器,服务器启动SpringBoot应用【SpringBootServletInitializer】,启动ioc容器;
Web开发
最新推荐文章于 2025-07-16 17:39:31 发布