<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency>
模板的的后缀必须是.ftlhtml
这里用做模板的地方是 职位详情页 用 el 表达式获取要替换模板的数据 <!--职位列表--> <div class="container nav-next-element" style="border: 1px solid #dcdcdc;padding-top: 30px;padding-bottom: 30px;"> <span> <img src="/imgs/join_us_title.jpg" alt=""> </span> <!--职位名称--> <div class="col-md-offset-1" style="padding-top: 30px;"> <img src="/imgs/join_us_icon.jpg" style="float: left;margin-right: 30px;" /> <h3>${title}</h3> </div> <!--职位描述--> <div class="col-md-offset-1" style="padding-top: 36px;"> <h4 style="float: left;margin-right: 30px;">职位描述:</h4> <div style="float: left;font-size: 14px;color: #575656;line-height: 30px;"> <p style="margin-top: 40px"></p> ${describes} </div> <div style="clear: both;"></div> </div> <!--任职要求--> <div class="col-md-offset-1" style="padding-top: 36px;"> <h4 style="float: left;margin-right: 30px;">任职要求:</h4> <div style="float: left;font-size: 14px;color: #575656;line-height: 30px;"> <p style="margin-top: 40px"></p> ${requires} </div> <div style="clear: both;"></div> </div> </div>
package cn.ydsat.util; import java.io.File; import java.io.PrintWriter; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import cn.itsource.domain.Job; import freemarker.template.Configuration; import freemarker.template.Template; public class FreemarkerUtil { //新生成静态页面 public static String Createhtml(Job job,HttpServletRequest req) throws Exception{ //获取保存静态网页的文件夹的真实路径 String realPath = req.getServletContext().getRealPath("/htmlftl"); //建立cfg对象 Configuration cfg = new Configuration(Configuration.VERSION_2_3_28); File file = new File(realPath); //设置模板存储地址 cfg.setDirectoryForTemplateLoading(file); //设置编码 cfg.setDefaultEncoding("UTF-8"); //准备对象 传入job时候就已经准备好了 //获取模板 Template template = cfg.getTemplate("jobmodel.ftl"); //使用随机数 随机生成名字的html静态页面 String newfile = UUID.randomUUID().toString()+".html"; //整合模板和数据 PrintWriter pw = new PrintWriter(new File(file,newfile)); template.process(job, pw); pw.close(); return "/htmlftl/"+newfile; } //删除静态页面 public static void deletehtml(Job job,HttpServletRequest req){ //获取工做对象中的静态化字段 /htmlftl/....,html String htmlurl = job.getHtmlurl(); //获取项目真实路径 String realPath = req.getServletContext().getRealPath(""); System.out.println(realPath); //路径和名字 新生成一个全新路径下文件 File file = new File(realPath,htmlurl); if(file.exists()){ file.delete(); } } }
//新增保存或者修改时 都要新建一个静态页面 ,修改还要将以前的静态化页面进行删除 @RequestMapping("/save") public String save(Job job,HttpServletRequest req) throws Exception{ Integer id = job.getId(); if (!"".equals(id) && id != null) { FreemarkerUtil.(job, req);//删除以前生成的静态html页面 String newhtmlurl = FreemarkerUtil.Createhtml(job, req);//根据页面传过来的值 重新生成一个静态页面 job.setHtmlurl(newhtmlurl); jobservice.edit(job); }else{ //新建一个静态页面,url做为字段属性保存到数据库 String htmlurl = FreemarkerUtil.Createhtml(job, req); job.setHtmlurl(htmlurl); jobservice.save(job); } return "...."; } //删除 同时删除以前的静态页面 @RequestMapping("/delete") public String delete(Integer id,HttpServletRequest req){ Job job = jobservice.queryOne(id);//查询出要删除id的工做信息 FreemarkerUtil.deletehtml(job, req); jobservice.delete(id); return "redirect:/job/queryAll"; }
因为以前将职位对象存放在了session.java
<a href="${job.htmlurl}">职位详情</a>