(五)Spring Boot配置静态资源访问,整合Thymeleaf模板
来源:互联网 发布:手机玩手游网络不稳定 编辑:程序博客网 时间:2024/04/28 21:38
配置静态资源的访问
spring boot默认是对静态资源做了映射的,默认配置的 /** 映射到 /static (或/public、/resources、/META-INF/resources)其中默认配置的 /webjars/** 映射到 classpath:/META-INF/resources/webjars/PS:上面的 static、public、resources 等目录都在 classpath: 下面(如 src/main/resources/static),也可以按照以前的 SpringMVC 工程 把静态资源放到webapp下也是可以访问的!如果你的应用将被打包成jar,那就不要使用src/main/webapp文件夹。尽管该文件夹是一个共同的标准,但它仅在打包成war的情况下起作用,并且如果产生一个jar,多数构建工具都会静悄悄的忽略它
如果你想增加如/mystatic/**映射到classpath:/mystatic/,你可以让你的配置类继承WebMvcConfigurerAdapter,然后重写如下方法:
@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/mystatic/**") .addResourceLocations("classpath:/mystatic/");}
使用thymeleaf模板
引入依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>spring-boot-starter-thymeleaf会自动包含spring-boot-starter-web,所以我们就不需要单独引入web依赖了。
配置视图解析器
Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。与其它模板引擎相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用。它的功能特性如下:
Spring MVC中@Controller中的方法可以直接返回模板名称,接下来Thymeleaf模板引擎会自动进行渲染
模板中的表达式支持Spring表达式语言(Spring EL)
表单支持,并兼容Spring MVC的数据绑定与验证机制
国际化支持
spring-boot 对于 Thymeleaf很多配置都有默认配置,比如:
默认页面映射路径为: classpath:/templates/
默认的文件后缀为: .html
默认的编码是: UTF-8
默认是开启页面缓存的:private boolean cache = true;
我们在使用Thymeleaf模板的时候,使用默认配置就可以了。只在开发环境的时候,把cache设置为false,避免看不到实时页面。在application.properties中可以配置thymeleaf模板解析器属性.就像使用springMVC的JSP解析器配置一样,Thymeleaf模板属性使用前缀是 spring.thymeleaf 具体可以配置的参数可以查看: org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties这个类,上面的配置实际上就是注入到该类中的属性值.
# THYMELEAF (ThymeleafAutoConfiguration)#开启模板缓存(默认值:true)spring.thymeleaf.cache=true #Check that the template exists before rendering it.spring.thymeleaf.check-template=true#检查模板位置是否正确(默认值:true)spring.thymeleaf.check-template-location=true#Content-Type的值(默认值:text/html)spring.thymeleaf.content-type=text/html#开启MVC Thymeleaf视图解析(默认值:true)spring.thymeleaf.enabled=true#模板编码spring.thymeleaf.encoding=UTF-8#要被排除在解析之外的视图名称列表,用逗号分隔spring.thymeleaf.excluded-view-names=#要运用于模板之上的模板模式。另见StandardTemplate-ModeHandlers(默认值:HTML5)spring.thymeleaf.mode=HTML5#在构建URL时添加到视图名称前的前缀(默认值:classpath:/templates/)spring.thymeleaf.prefix=classpath:/templates/#在构建URL时添加到视图名称后的后缀(默认值:.html)spring.thymeleaf.suffix=.html#Thymeleaf模板解析器在解析器链中的顺序。默认情况下,它排第一位。顺序从1开始,只有在定义了额外的TemplateResolver Bean时才需要设置这个属性。spring.thymeleaf.template-resolver-order=#可解析的视图名称列表,用逗号分隔spring.thymeleaf.view-names=
配置Controller
@Controllerpublic class TestThymeleafController {@RequestMapping("/index")public String getData(Model model){List<Student> list=new ArrayList<>();list.add(new Student("张三", 20, "北京"));list.add(new Student("李四", 30, "上海"));list.add(new Student("王五", 40, "河北"));list.add(new Student("赵六", 50, "山西"));model.addAttribute("list", list);return "/index";}}
编写html
在默认的模板路径src/main/resources/templates下编写模板文件即可完成。这里我们新建一个index.html: 使用thymeleaf模板,要修改html标签,添加<html xmlns:th="http://www.thymeleaf.org">,这样的话才可以在其他标签里面使用th:*这样的语法.
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head> <title>配置Thymeleaf模板</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><div style="text-align: center;margin:0 auto;width: 1000px; "><h1>配置Thymeleaf模板</h1><table width="100%" border="1" cellspacing="1" cellpadding="0"> <tr> <td>姓名</td> <td>年龄</td> <td>地址</td> </tr> <tr th:each="student : ${list}"> <td th:text="${student.name}"></td> <td th:text="${student.age}"></td> <td th:text="${student.address}"></td> </tr></table></div></body></html>访问http://localhost:8080/index
myeclipse安装Thymeleaf插件
为了使myeclipse Thymeleaf显示提示,使用离线解压ZIP方式,安装Thymeleaf插件
1.下载Thymeleaf插件:点击下载2.将文件解压到 myeclipse 根目录的 dropins 目录下 重启myeclipse 就有提示了
阅读全文
0 0
- (五)Spring Boot配置静态资源访问,整合Thymeleaf模板
- spring-boot--整合thymeleaf模板
- spring-boot--整合thymeleaf模板
- Spring Boot整合Thymeleaf模板引擎
- Spring Boot模板thymeleaf的基本配置
- spring boot 整合 thymeleaf
- Spring Boot整合thymeleaf
- spring boot访问静态资源
- Spring Boot+thymeleaf 静态资源上下文的问题
- spring boot 配置静态资源
- spring-boot--使用thymeleaf模板
- spring-boot--使用thymeleaf模板
- Spring Boot使用thymeleaf模板
- spring-boot--使用thymeleaf模板
- spring-boot--使用thymeleaf模板
- spring boot使用thymeleaf模板
- spring-boot thymeleaf模板学习
- spring-boot--使用thymeleaf模板
- 关于Ajax跨域提交时session丢失的问题
- eclipse修改编码为UTF-8的几处设置
- 逆水行舟 不进则退
- 文章标题
- 不懂带人,你就得自己干到死!(犀利)
- (五)Spring Boot配置静态资源访问,整合Thymeleaf模板
- 物理机内存模型
- 【二叉树】437. Path Sum III(理解递归)
- 软件设计师算法之分治法--归并排序
- 火狐SSL收到一个弱临时Diffie-Hellman连接失败解决方法
- 缓存淘汰算法--LRU算法
- Jquery之Ajax_分页及增删改查
- 月报7.25-8.24
- gulp配置文件