首页主机资讯springboot中freemarker怎么加载模板

springboot中freemarker怎么加载模板

时间2024-06-08 22:40:03发布访客分类主机资讯浏览479
导读:在Spring Boot应用中加载Freemarker模板需要进行以下步骤: 首先需要在pom.xml文件中添加Freemarker依赖: <dependency> <groupId>org.spring...

在Spring Boot应用中加载Freemarker模板需要进行以下步骤:

  1. 首先需要在pom.xml文件中添加Freemarker依赖:
<
    dependency>
    
    <
    groupId>
    org.springframework.boot<
    /groupId>
    
    <
    artifactId>
    spring-boot-starter-freemarker<
    /artifactId>
    
<
    /dependency>
    
  1. 创建Freemarker模板文件,通常会存放在src/main/resources/templates目录下。

  2. 在Spring Boot应用中创建一个Controller类,用于处理请求并返回Freemarker模板:

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


@Controller
public class HomeController {


    @GetMapping("/")
    public String home() {
    
        return "index";
 // 返回模板文件名,不需要加后缀
    }

}
    
  1. application.propertiesapplication.yml文件中配置Freemarker相关的属性,如模板文件的存放路径等。例如,在application.properties中添加以下配置:
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=.ftl
  1. 启动Spring Boot应用后,访问对应的URL即可加载并显示Freemarker模板。

注意:以上示例中的模板文件名为index.ftl,实际使用时可以根据需要创建不同的模板文件,并在Controller中返回对应的模板文件名。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: springboot中freemarker怎么加载模板
本文地址: https://pptw.com/jishu/678133.html
freemarker的ftl文件如何打开 Springboot生成pdf的方法是什么

游客 回复需填写必要信息