(二)SpringBoot2.0基础篇- 静态资源的访问及Thymeleaf模板引擎的使用
一、描述
在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等);
并且SpringBoot内置了Thymeleaf模板引擎,可以使用模板引擎进行渲染处理,默认版本为2.1,可以重新定义Thymeleaf的版本号,在maven的配置文件中配置如下内容:
<properties>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
二、默认静态资源的映射
Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:
- /static
- /public
- /resources
/META-INF/resources
SpringBoot默认会从META-INF/resources下的static、public、resources三个目录下查找对应的静态资源,而模板引擎的模板默认需要放在resources的templates目录下;
三、示例
1、静态资源的访问
- 创建maven项目,在resources目录下创建static、templates文件夹,将图片success.jpg放置在static中;
- 创建启动类,详情请看:(一)SpringBoot基础篇- 介绍及HelloWorld初体验;
- 启动项目,访问,http://localhost:8080/success.jpg,图片即可在页面展示成功;
2、Thymeleaf模板引擎
①、使用Thymeleaf前,需引入依赖类库:
<!-- 使用thymeleaf模板-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
②、创建启动类Application.java;
③、创建控制层HelloController.java;
package com.cn.controller;/**
* @Description: Created by xpl on 2018-05-01 13:23.
*/ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView; import java.util.HashMap; /**
* Created by xpl on 2018-05-01 13:23
**/ @RestController
public class HelloController { @RequestMapping("/getThymeleaf")
public ModelAndView getThymeleaf() {
ModelAndView modelAndView = new ModelAndView("hello");
modelAndView.addAllObjects(new HashMap<String, String>(){
{
this.put("name","Andy");
}
});
return modelAndView;
} }
④、创建Thymeleaf模板hello.html,访问变量使用th:进行访问;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
Title</title>
</head>
<body> <h1>Hello,</h1>
<h1 th:text="${name}"/>
</body>
</html>
⑤、启动项目,并访问http://localhost:8080/getThymeleaf,如下:
目录结构如下:
完整示例:https://gitee.com/lfalex/spring-boot-example
(二)SpringBoot2.0基础篇- 静态资源的访问及Thymeleaf模板引擎的使用的更多相关文章
- (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用
一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...
- SpringBoot静态资源访问+拦截器+Thymeleaf模板引擎实现简单登陆
在此记录一下这十几天的学习情况,卡在模板引擎这里已经是四天了. 对Springboot的配置有一个比较深刻的认识,在此和大家分享一下初学者入门Spring Boot的注意事项,如果是初学SpringB ...
- (六)SpringBoot2.0基础篇- Redis整合(JedisCluster集群连接)
一.环境 Redis:4.0.9 SpringBoot:2.0.1 Redis安装:Linux(Redhat)安装Redis 二.SpringBoot整合Redis 1.项目基本搭建: 我们基于(五) ...
- (四)SpringBoot2.0基础篇- 多数据源,JdbcTemplate和JpaRepository
在日常开发中,经常会遇到多个数据源的问题,而SpringBoot也有相关API:Configure Two DataSources:https://docs.spring.io/spring-boot ...
- (五)SpringBoot2.0基础篇- Mybatis与插件生成代码
SpringBoot与Mybatis合并 一.创建SpringBoot项目,引入相关依赖包: <?xml version="1.0" encoding="UTF-8 ...
- (一)SpringBoot2.0基础篇- 介绍及HelloWorld初体验
1.SpringBoot介绍: 根据官方SpringBoot文档描述,BUILD ANYTHING WITH SPRING BOOT (用SPRING BOOT构建任何东西,很牛X呀!),下面是官方文 ...
- (七)SpringBoot2.0基础篇- application.properties属性文件的解析及获取
默认访问的属性文件为application.properties文件,可在启动项目参数中指定spring.config.location的参数: java -jar myproject.jar --s ...
- (三)SpringBoot2.0基础篇- 持久层,jdbcTemplate和JpaRespository
一.介绍 SpringBoot框架为使用SQL数据库提供了广泛的支持,从使用JdbcTemplate的直接JDBC访问到完整的“对象关系映射”技术(如Hibernate).Spring-data-jp ...
- 【转】WF4.0 (基础篇)
转自:http://www.cnblogs.com/foundation/category/215023.html 作者:WXWinter —— 兰竹菊梅★春夏秋冬☆ —— wxwinter@16 ...
随机推荐
- [枫叶学院] Unity3d高级开发教程 工具集(一) 哈希列表——强大的自己定义数据集
在日常开发中.数据集合是我们不可缺少的重要工具之中的一个.在C#中,.Net Framework也为我们提供了种类繁多,功能多样的数据集工具.在此,我基于List<T> 和 HashTab ...
- 1、Android-活动(上)
1.1.活动是什么 活动(Activity)是最容易吸引用户的地方,他是一种可以包含用户界面的组件 主要用于和用户进行交互 一个用户可以包含零个或多个活动,不包含活动的程序少见 1.2.活动的基本用法 ...
- Graph I - Graph
Graph There are two standard ways to represent a graph G=(V,E)G=(V,E), where VV is a set of vertices ...
- svn .externals 属性
问:如下自定下载关联模块呢? 答:第一步: 编辑svn.externals文本,如下所示 dir/moduel_name URL/module dir/moduel_name URL/module 第 ...
- Linux Notes | Linux常用命令行笔记
[ show all running processes ] (1) ps -aux | less 'ps' means: Process Status The -a option tells ps ...
- 【hdu 3177 Crixalis's Equipment】 题解
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3177 \(describe\): 有一个山洞,山洞的容积最大为\(v\).现在你有\(n\)个物品,这 ...
- Linux-- su和sudo 切换用户
su 切换用户 用法:su [选项] [-] [用户 [参数]... ] - :以 login-shell 方式进行登录 不加 - :以 no-login-shell 方式进行登录 -c:只进行一次在 ...
- css一边固定,另一边自适应的方法
第一种: 第二种:
- zookeeper报错 JAVA_HOME is not set
很多开发者安装zookeeper的时候,应该会发现到这么一个问题: JAVA_HOME is not set 好的!那么这个是什么意思呢? 就是说你的 JAVA_HOME 变量没有设定 为什么会提示 ...
- 解决Vue中"This dependency was not found"的方法
今天在初始化项目中,出现了一个奇怪的情况:明明路径是对的,但是编译的时候,一直报"This dependency was not found"的错. 代码如下: import Vu ...