Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate thymleaf template engine in Springboot

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to integrate thymleaf template engine in Springboot". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Getting started with thymeleaf

1.1 introducing coordinates

Org.springframework.boot spring-boot-starter-thymeleaf

1.2 write controller classes

@ GetMapping ("/ index") public String index (Model model) {model.addAttribute ("msg", "hello"); return "index";}

1.3 Front-end page

This address must be added to the html tag in the page, otherwise thymeleaf cannot be used, and this URL can only be written in the html tag. If you add another URL, it will cause a page exception.

Exception:

Wang Chao

The result is that the content "Wang Chao" in the cite tag is replaced by hello.

Th:text is one of the grammars of thymeleaf, and its function is to replace text. Whether there is content in the tag or not, it will be replaced with the stored content. At the same time, we should also note that thymeleaf is relatively strict, if the label can not take the value, it will report an error.

Common thymeleaf notes are as follows:

In actual development, because ModelAndView is request-level, if you want to display data on other pages, you need to use session for storage. The most common thing is to display user information on the index page after login.

2. Thymeleaf uses session built-in objects (not recommended)

2.1 controller Class

HttpServletRequest request = HttpContextUtil.getHttpServletRequest (); request.getSession () .setAttribute ("user", (SysUser) SecurityUtils.getSubject () .getPrincipal ())

2.2 Front desk page

Wang Chao

The name of the account I logged in is admin, so Wang Chao in the tag will be replaced with admin. If you use model, you cannot get the value of username, and the page will report an error. So you need to use session for session storage, but thymeleaf does not recommend using built-in objects.

This is the end of "how to integrate thymleaf template engine in Springboot". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report