前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >FreeMarker整合Springmvc

FreeMarker整合Springmvc

作者头像
用户5927264
发布2019-08-01 10:10:24
6170
发布2019-08-01 10:10:24
举报
文章被收录于专栏:OSChinaOSChina

1 引用freddMarker jar包

代码语言:javascript
复制
<dependency>
	<groupId>org.freemarker</groupId>
	<artifactId>freemarker</artifactId>
</dependency>

2 在springmvc.xml文件中添加配置

代码语言:javascript
复制
	<!-- freeMarker整合 spring -->
	<bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
		<property name="defaultEncoding" value="UTF-8" />
	</bean>

3 编写controller

代码语言:javascript
复制
package com.shi.item.controller;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 
 * @author: SHF
 * @date: 2018年3月27日 上午11:45:21
 * @Description:通过freemarker生成静态文件
 */
@Controller
public class freeMarkerHtml {
	
	@Autowired
	private FreeMarkerConfigurer freeMarkerConfigurer;
	
	@RequestMapping("/createItemHtml")
	@ResponseBody
	public String createItemHtml()throws Exception{
		Configuration configuration = freeMarkerConfigurer.getConfiguration();
		//1 加载模版对象
		Template template = configuration.getTemplate("hello.ftl");
		//2 创建一个数据集
		Map map=new HashMap();
		map.put("hello", "你好,小小小施爷!!");
		
		//3 指定文件输出的文件路径及文件集
		Writer out=new FileWriter(new File("C:/Users/shiye/Desktop/hello2.html"));
		
		//4 输出文件
		template.process(map, out);
		//5 关闭流
		out.close();
		
		return "ok";
	}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档