springboot支持freemarker,需要在pom.xml文件中添加以下依赖:

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

  freemarker文件的默认路径是:classpath:/templates/,如下:

(016)Spring Boot之如何使用freemarker

   测试类如下:

package com.edu.spring.springboot;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class AccountController {

    @RequestMapping("/reg")
    public String reg(){
        return "/reg";
    }
    
}

  浏览器输入:http://127.0.0.1:8080/reg即可跳转到 reg.ftl页面

  修改freemarker的默认路径,在application.properties文件中添加spring.freemarker.templateLoaderPath属性,可以配置多个,逗号分隔,如下:

spring.freemarker.templateLoaderPath=classpath:/ftl/,classpath:/ftl2/

  目录结构如下:

(016)Spring Boot之如何使用freemarker

  测验类如下:

package com.edu.spring.springboot;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class AccountController {

    @RequestMapping("/reg")
    public String reg(){
        return "reg";
    }
    
    @RequestMapping("/logout")
    public String logout(){
        return "logout";
    }
}

  浏览器输入:http://127.0.0.1:8080/reg即可跳转到 reg.ftl页面

  浏览器输入:http://127.0.0.1:8080/logout即可跳转到 logout页面

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-23
  • 2021-06-02
  • 2021-12-19
  • 2021-11-04
  • 2022-12-23
  • 2021-04-19
猜你喜欢
  • 2022-12-23
  • 2021-07-04
  • 2021-07-23
  • 2021-11-18
  • 2021-12-13
  • 2021-08-06
相关资源
相似解决方案