SpringBoot 集成Freemarker

本文讲述简单的freemarker小案例搭建,更多详细关于freemarker点击文章结尾链接

创建环境

首先使用idea 创建项目,选择freemarker包
在这里插入图片描述

配置yml

在这里插入图片描述

代码

@Controller
public class HelloController {
    @RequestMapping("/")
    public ModelAndView hello(){
        ModelAndView modelAndView = new ModelAndView();
        People people = new People();
        people.setName("zhangsan");
        people.setSex("男");
        people.setAge(15);
        modelAndView.addObject("people", people);
        modelAndView.setViewName("index");
        return modelAndView;
    }
}

@Data
public class People {
    String name;
    String sex;
    Integer age;
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>name:${people.name}</h1>
<h1>sex:${people.sex}</h1>
<h1>age:${people.age}</h1>
</body>
</html>
运行结果

在这里插入图片描述
更多更详细的请看
springboot的两大常用模版:thymeleaf和freemarker

实现一个代码自动生成(一):模板引擎Freemarker

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot可以集成Freemarker作为视图格式,以便在Spring MVC中使用。为了创建一个Spring Boot Freemarker工程并测试模板,你需要执行以下步骤: 1. 创建一个测试工程,并在pom.xml文件中添加必要的依赖项。其中包括以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> ``` 2. 创建一个Controller类,在这个类中将需要的数据添加到Map中,并返回模板文件的名称。例如: ``` @Controller public class HelloController { @GetMapping("/basic") public String test(Model model) { // 纯文本形式的参数 model.addAttribute("name", "freemarker"); // 实体类相关的参数 Student student = new Student(); student.setName("小明"); student.setAge(18); model.addAttribute("stu", student); return "01-basic"; } } ``` 3. 创建一个启动类,使用@SpringBootApplication注解标记,并在main方法中运行Spring Boot应用程序。例如: ``` @SpringBootApplication public class FreemarkerDemotApplication { public static void main(String[] args) { SpringApplication.run(FreemarkerDemotApplication.class, args); } } ``` 4. 运行测试,访问对应的URL来查看Freemarker模板渲染的结果。 以上是使用Spring Boot集成Freemarker的基本步骤。你可以根据具体的需求和业务逻辑进行相应的调整和配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Springboot集成Freemarker|超级详细,建议收藏](https://blog.csdn.net/heima005/article/details/129435750)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Springboot整合FreeMarker](https://blog.csdn.net/zxy15974062965/article/details/122818338)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值