springBoot(5):web开发-模板引擎FreeMarker与thymeleaf

简介:

一、简介

spring boot的web应用开发,是基于spring mvc。


Spring boot在spring默认基础上,自动配置添加了以下特性:

 1、包含了ContentNegotiatingViewResolver和BeanNameViewResolver beans。

 2、对静态资源的支持,包括对WebJars的支持。

 3、自动注册Converter,GenericConverter,Formatter beans。

 4、对HttpMessageConverters的支持。

 5、自动注册MessageCodeResolver。

 6、对静态index.html的支持。

 7、对自定义Favicon的支持。

 8、主动使用ConfigurableWebBindingInitializer bean


二、模板引擎的选择

FreeMarker

Thymeleaf

Velocity (1.4版本之后弃用,Spring Framework 4.3版本之后弃用)

Groovy

Mustache

注:jsp应该尽量避免使用,原因如下:

 1、jsp只能打包为:war格式,不支持jar格式,只能在标准的容器里面跑(tomcat,jetty都可以)

 2、内嵌的Jetty目前不支持JSPs

 3、Undertow不支持jsps

 4、jsp自定义错误页面不能覆盖spring boot 默认的错误页面

三、FreeMarker使用

新建一个工程,勾选Freemarker、DevTools(开发方便)

wKioL1k_azHgXoASAABPQCG3DAQ480.jpg

会自动在pom.xml中加入Freemarker的配置:

1
2
3
4
< dependency >
    < groupId >org.springframework.boot</ groupId >
    < artifactId >spring-boot-starter-freemarker</ artifactId >
</ dependency >


WebController.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package  com.example.demo.controller;
 
import  org.slf4j.Logger;
import  org.slf4j.LoggerFactory;
import  org.springframework.stereotype.Controller;
import  org.springframework.ui.Model;
import  org.springframework.web.bind.annotation.RequestMapping;
 
/**
  * Created by DELL on 2017/6/13.
  */
@Controller
@RequestMapping ( "/web" )
public  class  WebController {
     private  static  final  Logger logger = LoggerFactory.getLogger(WebController. class );
 
     @RequestMapping ( "/index" )
     public  String index(Model model){
         logger.info( "这是一个controller" );
         model.addAttribute( "title" , "我是一个例子" );
         return  "index" ;   // 注意,不要再最前面加上/,linux下面会出错
     }
}

index.ftl:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
< html >
< head  lang = "en" >
    < title >Spring Boot Demo - FreeMarker</ title >
     < link  href = "/css/index.css"  rel = "stylesheet"  />
</ head >
< body >
     < center >
         < img  src = "/images/logo.png"  />
         < h1  id = "title" >${title}</ h1 >
     </ center >
 
     < script  type = "text/javascript"  src = "/js/jquery.min.js" ></ script >
 
     < script >
         $(function(){
             $('#title').click(function(){
                 alert('点击了');
             });
         })
     </ script >
</ body >
</ html >


说明:

 1、视图默认访问templates下面,如"index",则:在templates下面找index.ftl

 2、css、js、img等静态资源则在static下面找,如<link href="/css/index.css" rel="stylesheet" />,则是找static下面的css下面的index.css文件

四、thymeleaf模块

4.1、简介

Thymeleaf是一个优秀的面向java的XML/XHTML/HTML5页面模板,并且有丰富的标签语言和函数。使用Springboot框架进行界面设计,一般都会选择Thymeleaf模板。

4.2、使用

引入依赖:

1
2
3
4
< dependency >
    < groupId >org.springframework.boot</ groupId >
    < artifactId >spring-boot-starter-thymeleaf</ artifactId >
</ dependency >


配置application.properties文件:

1
2
3
4
5
6
7
8
#####################thymeleaf开始#####################################
#关闭缓存
spring.thymeleaf.cache=false
#后缀
spring.thymeleaf.suffix=.html
#编码
spring.thymeleaf.encoding=UTF-8
#####################thymeleaf结束#####################################

IndexController.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Controller
public  class  IndexController {
     @RequestMapping ( "/index" )
     public  String show(Model model)  throws  Exception {
         List<User> users =  new  ArrayList<User>();
         users.add( new  User(1L,  "zhangsan" ));
         users.add( new  User(2L,  "lisi" ));
         users.add( new  User(3L,  "wangwu" ));
         model.addAttribute( "hello" , "我只是一个例子" );
         model.addAttribute( "users" , users);
         model.addAttribute( "addtime" , new  Date());
         return "/helloHtml" ;
     }
}

main/resources/templates/helloHtml.html:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
< html  xmlns = "http://www.w3.org/1999/xhtml"  xmlns:th = "http://www.thymeleaf.org" >
< head >
     < title >Hello World!</ title >
</ head >< p  th:text = "${hello}" ></ p >
< body >
< h1  th:inline = "text" >Hello.v.2</ h1 >
< p  th:text = "${addtime} ? ${#dates.format(addtime, 'yyyy-MM-dd HH:mm:ss')}" ></ p >
< select >
     < option >请选择用户</ option >
     < option  th:each = "user:${users}"  th:value = "${user.id}"  th:text = "${user.name}" >
     </ option >
</ select >
</ body >
</ html >

浏览器中输入:http://localhost:8989/index ,效果如下:

wKiom1lZt7eSY8JCAABh1wZjjsw709.jpg

4.3、thymeleaf功能简介

在html页面中使用thymeleaf标签语言,用一个简单的关键字“th”来标注,如:

1
2
< p  th:text = "${hello}" ></ p >
< img  th:src = "@{images/logo.png}"  />

其中th:text指定在<p>标签中显示的文本,它的值来自"$"所引用的内存变量。

th:src指定img的图片地址


注意:使用th,需要<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

 wKiom1lcTuSxynbNAABWnFxuX6Y558.jpg


thymeleaf的主要标签和函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
th:text,显示文本。
th:utext,和th:text的区别是针对 "unescaped text" 。
th:attr,设置标签属性
th: if  or th: unless ,条件判断语句
th:switch, th:case,选择语句
th: each ,循环语句
 
#date:日期函数
#calendars:日历函数
#numbers:数字函数
#strings:字符串函数
#objects:对象函数
#bools:逻辑函数
#arrays:数组函数
#lists:列表函数

详细标签请查看官方:http://www.thymeleaf.org/  

本文转自我爱大金子博客51CTO博客,原文链接http://blog.51cto.com/1754966750/1934891如需转载请自行联系原作者


我爱大金子

相关文章
|
前端开发 Java 数据库
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 介绍
本课介绍Spring Boot集成Thymeleaf模板引擎。Thymeleaf是一款现代服务器端Java模板引擎,支持Web和独立环境,可实现自然模板开发,便于团队协作。与传统JSP不同,Thymeleaf模板可以直接在浏览器中打开,方便前端人员查看静态原型。通过在HTML标签中添加扩展属性(如`th:text`),Thymeleaf能够在服务运行时动态替换内容,展示数据库中的数据,同时兼容静态页面展示,为开发带来灵活性和便利性。
721 0
|
缓存 自然语言处理 Java
详解FreeMarker Template:在Spring Boot中实现动态内容生成
详解FreeMarker Template:在Spring Boot中实现动态内容生成
3085 13
|
Java API 数据库
构建RESTful API已经成为现代Web开发的标准做法之一。Spring Boot框架因其简洁的配置、快速的启动特性及丰富的功能集而备受开发者青睐。
【10月更文挑战第11天】本文介绍如何使用Spring Boot构建在线图书管理系统的RESTful API。通过创建Spring Boot项目,定义`Book`实体类、`BookRepository`接口和`BookService`服务类,最后实现`BookController`控制器来处理HTTP请求,展示了从基础环境搭建到API测试的完整过程。
801 4
|
XML JSON Java
【SpringBoot(三)】从请求到响应再到视图解析与模板引擎,本文带你领悟SpringBoot请求接收全流程!
Springboot专栏第三章,从请求的接收到视图解析,再到thymeleaf模板引擎的使用! 本文带你领悟SpringBoot请求接收到渲染的使用全流程!
856 3
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
1220 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
JSON 前端开发 Java
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 的使用
本文介绍了 Thymeleaf 在 Spring Boot 项目中的使用方法,包括访问静态页面、处理对象和 List 数据、常用标签操作等内容。通过示例代码展示了如何配置 404 和 500 错误页面,以及如何在模板中渲染对象属性和列表数据。同时总结了常用的 Thymeleaf 标签,如 `th:value`、`th:if`、`th:each` 等,并提供了官方文档链接以供进一步学习。
1090 0
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 的使用
|
Java 开发者 微服务
Spring Boot 入门:简化 Java Web 开发的强大工具
Spring Boot 是一个开源的 Java 基础框架,用于创建独立、生产级别的基于Spring框架的应用程序。它旨在简化Spring应用的初始搭建以及开发过程。
1376 7
Spring Boot 入门:简化 Java Web 开发的强大工具
|
移动开发 前端开发 JavaScript
SpringBoot3 整合Thymeleaf 模板引擎
Thymeleaf 是一个基于 Java 的现代模板引擎,支持 HTML 原型,文件后缀为 .html,可直接在浏览器中查看静态效果。它与 Spring Boot 完美整合,默认配置即可使用,无需额外视图解析器设置。Thymeleaf 支持多种表达式(如变量、链接、国际化等)和 th 属性(如 th:text、th:if 等),适用于 Web 和非 Web 应用开发。通过 th:fragment、th:insert、th:replace 和 th:include 等属性,可以抽取和复用公共页面片段,并支持参数传递。
2326 12
|
缓存 Java 应用服务中间件
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——依赖导入和Thymeleaf相关配置
在Spring Boot中使用Thymeleaf模板,需引入依赖`spring-boot-starter-thymeleaf`,并在HTML页面标签中声明`xmlns:th=&quot;http://www.thymeleaf.org&quot;`。此外,Thymeleaf默认开启页面缓存,开发时建议关闭缓存以实时查看更新效果,配置方式为`spring.thymeleaf.cache: false`。这可避免因缓存导致页面未及时刷新的问题。
681 0
|
XML Java 网络架构
使用 Spring Boot 公开 SOAP Web 服务端点:详细指南
使用 Spring Boot 公开 SOAP Web 服务端点:详细指南
2134 0

热门文章

最新文章