sprin boot使用freemarker模板,并将模板内容以字符串形式返回

1、引入依赖

<!-- 引入freeMarker的依赖包. -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>

2、创建模板文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发送邮件测试</title>
</head>
<body>
<p>内容:${content}</p>
<p>日期:${time?date}</p>
</body>
</html>

注意,这里的${time?date}表示time是日期类型的变量,只取date部分。“?date”还可以使用“?datetime”或“?time”。

3、使用模板引擎

	@Autowired
    FreeMarkerConfigurer configurer;//模板引擎

    @Test
    public String index(Map map) throws IOException, TemplateException {
        map.put("content","哈哈哈");
        map.put("time",new Date());
        Template template = configurer.getConfiguration().getTemplate("mail.ftl");
        String resp = FreeMarkerTemplateUtils.processTemplateIntoString(template,map);
        return resp;
    }

附:使用循环遍历一个List的模板:(转)

<html>  
<body>  
    <h3>发现错误!<a href="${errorLogLink}" target="_blank">点击这里查看详情</a></h3>  
      
    <h3>错误列表:</h3>  
      
    <table border="1px solid #8968CD" style="border-collapse: collapse;"><tr><th>错误位置</th> <th>数量</th> <th>错误信息</th> <th>错误类名</th> <th>更多信息</th> </tr>  
    <#list errorList as error>  
        <tr>  
            <td>${error.pos}</td>  
            <td>${error.count}</td>  
            <td>${error.msg}</td>  
            <td>${error.eName!}</td>  
            <td>${error.details!}</td>  
        </tr>  
    </#list>  
    </table>  
      
</body>  
</html> 

注意:最后那个两个感叹号表示:如果error.eName/error.details的值为空,则用空格代替。感叹号后面也可以加一个缺省字符串,在空值的时候代替空值。如果不加感叹号会报错。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot整合Freemarker可以方便地进行页面模板的渲染,下面是详细的教程: 1. 新建Spring Boot项目 首先需要创建一个Spring Boot项目,可以使用IDEA或者Eclipse等开发工具创建。 2. 添加依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> ``` 这样就可以使用Freemarker了。 3. 配置Freemarker 在application.properties文件中添加以下配置: ``` # 设置模板文件所在的路径 spring.freemarker.template-loader-path=classpath:/templates/ # 设置模板文件的后缀名 spring.freemarker.suffix=.ftl ``` 这里将模板文件放在了classpath:/templates/目录下,后缀名为.ftl。 4. 创建模板文件 在classpath:/templates/目录下创建一个模板文件,例如:index.ftl。 在模板文件中可以使用Freemarker的语法进行页面渲染,例如: ``` <!DOCTYPE html> <html> <head> <title>Freemarker Example</title> </head> <body> <h1>Hello, ${name}!</h1> </body> </html> ``` 这里使用了${name}变量进行字符串替换,后面会在Java代码中传入这个变量的值。 5. 创建Controller 在Java代码中创建一个Controller类,例如: ``` @Controller public class IndexController { @GetMapping("/") public String index(Model model) { model.addAttribute("name", "Freemarker"); return "index"; } } ``` 这里使用了@GetMapping注解将请求映射到"/"路径,将"name"变量的值设为"Freemarker"并传入模板文件中进行渲染,返回index.ftl文件名。 6. 运行项目 启动Spring Boot项目后访问http://localhost:8080/,就可以看到页面上显示了"Hello, Freemarker!"。 这就是Spring Boot整合Freemarker的详细教程,希望对你有帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值