controller :
@RequestMapping(value = "/main", method = RequestMethod.GET)
public String main(Model model){
String w="Welcome fusiping FreeMarker!";
Map root = new HashMap();
root.put("w",w);
freeMarkerContent(root);
return "web/test.html";
}
private void freeMarkerContent(Map<String,Object> root){
try {
Template temp = cfg.getTemplate("test.ftl");
//以classpath下面的static目录作为静态页面的存储目录,同时命名生成的静态html文件名称
// String path=ClassUtils.getDefaultClassLoader().getResource("").getPath(); ///E:/wkcq_java4/ltu-portal/ltu-mng/target/test-classes/ftl/test.html
Writer file = new FileWriter(new File("E:/wkcq_java4/ltu-portal/ltu-mng/src/main/webapp/WEB-INF/views/web/test.html"));
// Writer out = new FileWriter(new File("d:/freemarker/index.html"));
temp.process(root, file);
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
}
test.ftl:
<html>
<head>
<title>Welcome!</title>
<link rel="stylesheet" href="/bootstrap.min.css">
<script src="/lib/jquery.min.js"></script>
</head>
<body>
<h1>Hello ${w}!</h1>
</body>
</html>
依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>