SpringBoot学习11之thymeleaf

第一步:项目搭建

1.点击新建,然后如下图所示设置

在这里插入图片描述

2.Web选用Spring Web,Template Engines选用Thymeleaf

在这里插入图片描述
在这里插入图片描述

3.然后点击next

4.继续点击finish

在这里插入图片描述

5.项目目录如下图所示

在这里插入图片描述

第二步:配置application.properties文件

#配置端口号
server.port=8080


#配置thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML

上面配置了端口号为8080,thymeleaf中的设置,自动为文件名添加后缀,并且说明了文件的存放位置和文件的编码。

第三步:再com.pp.thyleaf下创建bean包,然后创建Student实体类

package com.pp.thyleaf.bean;

import java.util.Date;

public class Student {
    private String name;
    private Integer age;
    private Date birthday;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

第四步:再com.pp.thyleaf下创建controller包,然后创建ThController实体类

package com.pp.thyleaf.controller;

import com.pp.thyleaf.bean.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Controller
public class ThController {
    @RequestMapping("/th")
    public String test(ModelMap map){
        Student student = new Student();
        student.setAge(11);
        student.setName("zhangsan");
        student.setBirthday(new Date());
        map.addAttribute("student",student);
        return "test";
    }
}

如上代码所示:

1. 先使用@Controller对类进行了注解,

2. 然后使用@RequestMapping("/th")对方法test()进行注解,这样就可以在浏览器中输入localhost:8080/th就可以运行下面的方法.

3. test的参数为ModelMap类型的map,因为这里处理的是个类对象。

4. 方法里面先创建一个Student对象,然后使用set方法赋值

5. 接下来使用 map.addAttribute(“student”,student)将student对象的值存放到map中,并且重新命名为student

6. 最后使用return "test"返回到test.xml页面中

第五步:在resource/templates下创建test.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <td>姓名</td>
        <td >年龄</td >
        <td>生日</td>
    </tr>
    <tr>
        <td th:text="${student.name}"></td>
        <td th:text="${student.age}"></td>
        <!--这里是一个日期,需要对它进行处理。-->
        <!--th:text="${#dates.format(user.date, 'yyyy-MM-dd')}"-->
        <td th:text="${#dates.format(student.birthday,'yyyy-MM-dd')}"></td>
    </tr>
</table>
</body>
</html>

test.html中页面使用了表格,在表格里面使用对后台传送过来的值进行了读取,在表格<td>中使用th:text="${student.name}"直接可以将后台的student的name输出出来。因为后台传递过来的student.age是一个Date类型的值,返回到前台不方便直观看到日期,所以使用#dates.format(student.birehday,‘yyyy-MM-dd’)对返回过来的日期进行格式化输出。

第七步:页面展示

在这里插入图片描述
源码地址:https://gitee.com/yangforever/project-learning/tree/master/demo/SpringBoot/thyleaf

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值