1.导入插件到eclipse安装目录dropins
2.在需要使用模板的项目的web-inf文件夹下创建文件夹保存模板文件
3.模板编写,简单模板
<link rel="stylesheet" type="text/css" href="css/promotion_detail.css">
<div class="container promotions" >
<div class="col-md-2 prolist">
<h5 class="title"><a href="#/promotion"><strong>返回促销列表</strong></a></h5>
<img src="images/pro.jpg" class="img-responsive">
</div>
<div class="col-md-10 procontent">
<h5 class="title">${promotion.title}</h5>
<div class="intro">
<p>活动范围:${promotion.activeScope}</p>
<p>活动时间: ${promotion.startDate?string(yyyy-MM-dd)} -
${promotion.endDate?string(yyyy-MM-dd)}</p>
</div>
<div class="partline clearfix"></div>
<div class="promotionbox">
${promotion.description}
</div>
</div>
</div>
4.创建模板生产文件夹
5.查询数据库,生产数据,写入模板文件,生产静态页面(结合AngularJS路由)
5.1 页面路径传递查询数据的id
5.2路由页面接收参数
5.3编写action
@Action(value="promotion_html")
public String creatHtml() throws IOException, Exception {
//1.获取写出模板文件地址
String html = ServletActionContext.getServletContext().getRealPath("promotion");
//2.创建文件
File htmlFile = new File(html,model.getId()+".html");
if(!htmlFile.exists()) {
//1.获取模板存放路径
String file = ServletActionContext.getServletContext().getRealPath("WEB-INF/freemarkertemplate");
//2.设置版本
Configuration configuration = new Configuration(
Configuration.VERSION_2_3_22);
//3.加载模板地址
configuration.setDirectoryForTemplateLoading(new File(file));
//4.加载模板
Template template = configuration.getTemplate("freemarker.ftl");
//5.查询数据,调用webservice服务获取其他服务器数据
Promotion promotion = WebClient.create("http://localhost:8081/bos_management/services/promotionService/getPromotion/"+model.getId()).accept(MediaType.APPLICATION_JSON).get(Promotion.class);
Map<String, Object> parameterMap = new HashMap<String, Object>();
parameterMap.put("promotion", promotion);
template.process(parameterMap,new FileWriter(htmlFile));
}
//6.把生产页面响应回页面
FileUtils.copyFile(htmlFile, ServletActionContext.getResponse().getOutputStream());
return NONE;
}