您好,登錄后才能下訂單哦!
springboot中如何使用freemarker頁面模版,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
springboot默認使用thymeleaf模版,但是性能不如freemarker,對比兩者freemarker語法更簡單,所以在這里我們使用freemarker作為我們的頁面渲染引擎
引入pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
application.properties加入freemarker配置信息
## Freemarker 配置
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
在views目錄下新建freemarker的ftl文件即可
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;
}
}
在views里新建user.ftl 內(nèi)容如下
<!DOCTYPE html>
<html>
<body>
id: ${user.id} <br>
name:${user.userName}
</body>
</html>
啟動application,在瀏覽器輸入http://localhost:8888/sys/user/getUserById/1 頁面展示
id: 1
name:goodluck
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。