温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring boot项目如何使用thymeleaf模板

发布时间:2020-07-30 13:35:20 来源:亿速云 阅读:146 作者:小猪 栏目:开发技术

小编这次要给大家分享的是Spring boot项目如何使用thymeleaf模板,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

在spring boot 项目中使用thymeleaf模板,将后台数据传递给前台界面。

1、将后台数据传递给前台有很多种方式,可以将后台要传递的数据转换成json格式,去传递给前台,也可以通过model形式去传递出去,这篇博客主要是使用thymeleaf模板,将后台数据传递给前台。

2、首先要在spring boot 项目中添加如下依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3、这里后台有关如何查询数据,得到数据的具体过程就不在多说了,只是写将数据库中查询到的数据取出来,放到model里面。这里就一个例子吧。

@RequestMapping("/")
public String index(Model model){

Person single=new Person("aa",11);

List<Person> people =new ArrayList<Person>();
Person p1=new Person("xx",22);
Person p2=new Person("dd",33);
Person p3=new Person("zz",44);

people.add(p1);
people.add(p2);
people.add(p3);

model.addAttribute("singlePerson",single);
model.addAttribute("people",people);

return "index";
}

4.前台界面的写法,

<span th:text="${person.name}"></span> <span th:text="${person.age}"></span>

通过这样的方法就可以取到放入model中的person的name和age了。

看完这篇关于Spring boot项目如何使用thymeleaf模板的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI