溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

springboot中如何使用freemarker頁面模版

發(fā)布時間:2021-11-10 09:49:57 來源:億速云 閱讀:192 作者:柒染 欄目:大數(shù)據(jù)

springboot中如何使用freemarker頁面模版,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

springboot默認使用thymeleaf模版,但是性能不如freemarker,對比兩者freemarker語法更簡單,所以在這里我們使用freemarker作為我們的頁面渲染引擎

配置使用教程
  1. 引入pom

    <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-freemarker</artifactId>
   </dependency>
  1. 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
  1. 在views目錄下新建freemarker的ftl文件即可

測試
  1. 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;
   }
}
  1. 在views里新建user.ftl 內(nèi)容如下

<!DOCTYPE html>
<html>
<body>
   id: ${user.id} <br>
   name:${user.userName}
</body>
</html>
  1. 啟動application,在瀏覽器輸入http://localhost:8888/sys/user/getUserById/1 頁面展示 
    id: 1 
    name:goodluck

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI