- In eclipse the click Window , select Reference , the following pop-up box
- Found : General -> Editors -> File Associations
As follows : If there are * .ftl file, do not click on the above the Add . If not, click Add New
- Then click on this page below the Add button:
- Then select General - Content Types :
-
Programming steps, where we write a utility class to help us use
-
Package cn.jiedada.util; Import java.io.File; Import java.io.IOException; Import java.io.PrintWriter; Import a java.io.Writer; Import java.util.UUID; Import freemarker.template.Configuration; Import freemarker.template.Template; public class FreeMakerUtil { / ** * @param the templatePath - FTL parent directory path file * @param templateName - FTL file name * @param Object - incoming data * @param suffix - file suffix * @return * / Public static String CreateFile (the templatePath String, String templateName, Object Data, String suffix) { // create a configuration object the Configuration = the conf new new the Configuration (Configuration.VERSION_2_3_28); Writer PW = null ; String htmluri = null ; the try { / / create a parent directory file file file = new new file (the templatePath); // set the default template file path and character set conf.setDirectoryForTemplateLoading (file); conf.setDefaultEncoding ( "UTF-8"); // create Template Template Template = conf.getTemplate (templateName); // set the new name String uuid = UUID.randomUUID () toString () the replace ( "-", ".". ); Htmluri = uuid + suffix; // set the output path PW = new new the PrintWriter ( new new File (File, htmluri)); template.process (Data, PW); } the catch (Exception E) { // the TODO Auto-Generated Block the catch e.printStackTrace (); } a finally { if(pw!=null){ try { pw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return htmluri; } }
Incoming data can only map or entity class
- and entity class map traversal
- Entity class field names directly
- a list object is added to a plurality of entities in the map traversal
-
< HTML > < head > < Meta charset = "UTF-. 8" > </ head > < body > data // passed in the map as a single data our < #list List as L > City: $ {l .cname} city level < #if L.ID lt 10 > tier cities < #ElseIf L.ID lt 21 is > second city < #else > three cities </ #if > </ #list > </body> </html>
So we need to modify and delete data deleted when the project what we need to do, because we delete, and update stored into the address of html
- Here is a modification
-
@RequestMapping("/modify") public String modify(Jobs jobs,HttpServletRequest req){ //先删除url在修改 String realPath = req.getServletContext().getRealPath("/freemakser"); File file = new File(realPath,jobs.getHtmlurl()); if(file.exists()){ file.delete(); } service.modify(jobs); return "forward:page"; }
Here is deleted
-
@RequestMapping("/del") public String delete(Integer id,String htmlurl,HttpServletRequest req){ service.del(id); String realPath = req.getServletContext().getRealPath("/freemakser"); File file = new File(realPath,htmlurl); if(file.exists()){ file.delete(); } return "forward:page"; }
service may be modified to
-
@Override public void modify(Jobs jobs) { String templatePath = "F:\\JAVAEE\\2019_09_11_cms4\\src\\main\\webapp\\freemakser"; String templateName ="join_us_details.ftl"; String htmlurl = FreeMakerUtil.createFile(templatePath, templateName, jobs, ".html"); jobs.setHtmlurl(htmlurl); dao.modify(jobs); } @Override public void add(Jobs jobs) { String templatePath = "F:\\JAVAEE\\2019_09_11_cms4\\src\\main\\webapp\\freemakser"; String templateName ="join_us_details.ftl"; String htmlurl = FreeMakerUtil.createFile(templatePath, templateName, jobs, ".html"); jobs.setHtmlurl(htmlurl); dao.add(jobs); }