必須添加以下兩個jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
sringmvc.xm
<!-- freemarker的配置 -->
<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
<property name="defaultEncoding" value="utf-8"></property>
</bean>
controller代碼
@Controller
public class HtmlGenController {
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@RequestMapping("/genhtml")
@ResponseBody
public String genHtml() throws Exception {
//生成靜態頁面
Configuration configuration = freeMarkerConfigurer.getConfiguration();
Template template = configuration.getTemplate("hello.ftl");
Map data = new HashMap<>();
data.put("hello", "spring freemarker test");
Writer out = new FileWriter(new File("D:/temp/javaee28/out/test.html"));
template.process(data, out);
out.close();
//返回結果
return "OK";
}
}