Freemaker初接触(二) 简单生成html文件

这篇博客介绍了如何在不考虑与SpringMVC集成的情况下,利用FreeMarker工具类生成HTML文件。文章提供了一个自定义的FreeMarkerUtil类,该类包含了创建配置、获取模板并填充参数到HTML文件的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们的项目在本次试用的时候,不用考虑和springMVC的集成时的页面展示问题,而只需要考虑生成文件的问题。


自己写的工具类

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreeMakerUtil {
 /**
  * @param freeMarkerConfigurer
  * @param parms
  * @param templateName
  * @param htmlPath
  * @param htmlName
  * @throws IOException
  * @throws TemplateException
  */
 public static void useFreemakerMakerHtml(FreeMarkerConfigurer freeMarkerConfigurer, Map<String, Object> parms, String templateName, String htmlPath,String htmlName) throws IOException, TemplateException {
  Writer out = null;
  try {
   File file = new File(htmlPath);
   if(!file.exists()){
    if(file.isDirectory()){
     file.mkdirs();
    }else{
     file.mkdir();
    }
   }
   Configuration configuer = freeMarkerConfigurer.getConfiguration();
   Template template = configuer.getTemplate(templateName);
   String fileName =htmlPath+htmlName;
   File file1 = new File(fileName);
   if(file1.exists()){
    file1.delete();
   }
   out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlPath+"/"+htmlName), "UTF-8"));
   template.process(parms, out);
   out.flush();
  } catch (IOException e) {
   e.printStackTrace();
   throw e;
  } catch (TemplateException e) {
   e.printStackTrace();
   throw e;
  } finally {
   if (out != null) {
    try {
     out.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }

}
工具类的使用
@Override
public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
 Map<String,Object> parms =getParms();
 String htmlPath = "D:/WEB-INF/view/";
 String htmlName ="test1.html";
 String templateName ="freemaker.ftl";
// String templateName1 ="freemaker1.ftl";
// String htmlName1 ="test2.html";
 FreeMakerUtil.useFreemakerMakerHtml(freeMarkerConfigurer, parms,  templateName, htmlPath, htmlName);
// FreeMakerUtil.useFreemakerMakerHtml(freeMarkerConfigurer, parms,  templateName1, htmlPath, htmlName1);
 return new ModelAndView();
}
public Map<String,Object> getParms(){
 Map<String,Object> parms = new HashMap<String, Object>();
 parms.put("a2", "test1");
 User user1 = new User(1,1,"1");
 parms.put("a1", user1);
 ArrayList<User>users = new ArrayList<User>();
 User user2 = new User(2,2,"2");
 User user3 = new User(3,3,"3");
 User user4 = new User(4,4,"4");
 users.add(user2);
 users.add(user3);
 users.add(user4);
 parms.put("userList", users);
 User testNullUser =new User(5,5,"test5");
 parms.put("testNullUser", testNullUser); 
parms.put("testNullStr","");
 return parms;
}
实体类 实体类的使用,我今天犯的错误是,为了方便测试,我将类写成了内部类,而不是public,这样写会造成,在模板文件中,比如遍历list的时候,能找到对象,但是对象里的值都会为空,(这个问题浪费了我将近一小时的时间)。


public class User{
 private int age;
 private int id;
 private String name;
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public User(int age, int id, String name) {
  this.age = age;
  this.id = id;
  this.name = name;
 }
}
重点:
模板文件的使用
<html>
<head >
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
</head>
<table>
 this is the others ftl   
 <tr>
     <th>sigleUser</th>
  <td>${a1.name}</td>
  <td>${a1.age}</td>
  <td>${a1.id}</td>
 </tr>   
 <tr>
  <#list userList as user>
   <tr>
   <td>
    id:-------:${user.id}
   <#if user.id==2> user id是${user.id}
   <#elseif  user.name=="2">
    user的名字是${user.name} 
   <#else>
    user的年纪是${user.age}   
   </#if>
   </td>
   </tr>
  </#list>
 </tr>
 <br>
 <tr>
  <#if a2!="test1">
   this is compare to str ,use operater is "==" a2 value is ${a2}
   <#else >
    this is compare to str a2 value is  not ${a2} 
  </#if>
  <br>
  判断是否为空值 用两个??进行判断,注意,else里面才是处理的为空的情况
  <#if testNullUser.nullValue??>
   值不为空
  <#else>
   <br>
   对空值进行处理的方式为在取值后加上!"XXX",在XXX的值为预设值 ${testNullUser.nullValue!"Set this value is NullValue"}
  </#if>  
 </tr>
 <tr>
  <#if testNullUser.atongmu ??>
   testNullUser.atongmu 不是空值
  <#else>
   ${testNullUser.atongmu!"网卡卡就不是空值了"}
  </#if>

<#if testNullStr??>
   对于值为  “”的空串,freemaker不认为其为空 ----------
   <br>
  <#else> 
   <br>
   对于值为  “”的空串,freemaker认为其为空 ${testNullStr!"认为为空"}

 </tr>
 
</table> 
</html>

ftl的取值方式简单的是:${XX.xx.xx},具体看我写的例子,不赘述了。

freemaker 的过程控制语句为<#XX>格式如<#if ><#esle >等等
在过程控制中,难免要遇到比较,freemaker对于数字和字符串的比较都是==
特殊一点的写法,对于空值的判断
??就判断了这个值是否为空
<#if XX??>
<#else>

${XX! "YY"} !后面的“YY”为值为空的时候的预设值
</#if>
<#if>需要结束符,<#else>不需要

最后运行生成的html为:

<html>
<head >
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
</head>
<table>
 this is the others ftl   
 <tr>
     <th>sigleUser</th>
  <td>1</td>
  <td>1</td>
  <td>1</td>
 </tr>   
 <tr>
   <tr>
   <td>
    id:-------:2
    user id是2
   </td>
   </tr>
   <tr>
   <td>
    id:-------:3
       user的年纪是3   
   </td>
   </tr>
   <tr>
   <td>
    id:-------:4
       user的年纪是4   
   </td>
   </tr>
 </tr>
 <br>
 <tr>
    this is compare to str a2 value is  not test1 
  <br>
  判断是否为空值 用两个??进行判断,注意,else里面才是处理的是不为空的值
   <br>
   对空值进行处理的方式为在取值后加上!"XXX",在XXX的值为预设值 Set this value is NullValue
 </tr>
 <tr>
   网卡卡就不是空值了
  <br>
  
  对于值为  “”的空串,freemaker不认为其为空 ----------
   <br>
 </tr>
 </table> 
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值