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 use freemarker Page template in springboot

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Springboot how to use the freemarker page template, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Springboot uses thymeleaf template by default, but its performance is not as good as freemarker, so it is easier to compare the freemarker syntax between the two, so here we use freemarker as our page rendering engine

Configuration usage tutorial

Introduction of pom

Org.springframework.boot

Spring-boot-starter-freemarker

Application.properties joins freemarker configuration information

# # Freemarker configuration

Spring.freemarker.template-loader-path=classpath:/views/

Spring.freemarker.cache=false

Spring.freemarker.charset=UTF-8

Spring.freemarker.check-template-location=true

Spring.freemarker.content-type=text/html

Spring.freemarker.expose-request-attributes=true

Spring.freemarker.expose-session-attributes=true

Spring.freemarker.request-context-attribute=request

Spring.freemarker.suffix=.ftl

Just create a new ftl file for freemarker in the views directory.

test

Configure in controller

@ Controller

@ RequestMapping ("/ sys/user/")

Public class UserController {

@ Autowired

SysUserService userService

@ RequestMapping (value = "getUserById/ {id}", method = RequestMethod.GET)

Public ModelAndView getUserById (@ PathVariable ("id") Long id) {

ModelAndView mv = new ModelAndView ("user")

Mv.addObject ("user", userService.getUserById (id))

Return mv

}

}

Create a new user.ftl in views as follows

Id: ${user.id}

Name:$ {user.userName}

Start application and display it on the browser input http://localhost:8888/sys/user/getUserById/1 page

Id: 1

Name:goodluck

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report