springboot整合Thymeleaf入门

一、简介

Thymeleaf是一款用于渲染XML、XHTML、HTML5内容的模板引擎。类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。

官网:https://www.thymeleaf.org/

二、使用方法

1、pom.xml中引入Thymeleaf依赖

2.、在resources目录下创建templates目录,存放.html模板文件。

       在resources目录下创建static目录,静态资源文件(图片、css、js等)。

3、创建.html模板文件

注意点:

a.模板文件头部使用<html xmlns:th="http://www.thymeleaf.org">定义。

b.模板文件所有标签必须闭合

c 标签上使用th:开头标识作为前缀

d.使用@{}引入web静态文件

如:<link rel="stylesheet" th:href="@{/css/test.css}"/>

       <script type="text/javascript"   th:src="@{/js/jquery.min.js}"></script>

e. 页面接受数据,获取model中数据,使用thymeleaf语法

三、demo实例 

 1、添加依赖

       <!-- thymeleaf模板引擎 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2、创建上述文件夹目录 

3、创建test.html页面 

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>测试Thymeleaf模板引擎</title>
<link rel="stylesheet" th:href="@{/css/test.css}"/>
</head>
<body>
    <h2 id="bt">测试列表</h2>
    <div>
        <ul>
            <li th:each="user:${userList}">
                <span th:text="${user.userId}"></span>-
                <span th:text="${user.userName}"></span>- 
                <span th:text="${user.userAge}"></span>-
                <span th:text="${user.userSex}"></span>
            </li>
        </ul>
    </div>
</body>
</html>

4. controller

@Controller
public class TestThymeleaf {

    
    @Autowired
    private UserDao dao;
    
    @RequestMapping(value = "/getUserList", method = RequestMethod.GET)
    public String get(Model model) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> rspmap = new HashMap<String, Object>();
        map.put("userId", "1");
        List<bootUser> users = dao.getUserInfo(map);
        model.addAttribute("userList", users);
        return "/test/test"; 
    }
}

5. 访问http://localhost:8888/index/getUserList.do,出现如下页面

 

-----spring整合 Thymeleaf配置

1.添加依赖

        <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>3.0.2.RELEASE</version>
        </dependency>

2、xml配置(配置jsp视图解析器替换成一下配置)

<!-- 配置thymeleaf视图解析器-->
    <bean id="templateResolver"
        class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML" />
        <property name="cacheable" value="false" />
        <property name="characterEncoding" value="UTF-8"/>
    </bean>
    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
        <property name="enableSpringELCompiler" value="true" />
    </bean>
    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="order" value="1" />
        <property name="templateEngine" ref="templateEngine" />
        <!--解决中文乱码--> 
        <property name="characterEncoding" value="UTF-8"/>
    </bean>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值