使用Velocity模板引擎生成HTML

 动态内容生成HTML.好处大家应该在网上都略微了解了些.下面我向大家介绍一下,如何使用Velocity生成HTML.

 

关于中文问题,我已经在另一篇里介绍过了,该如何设置Velocity的编码,这里就不再重复了.

代码见下:

  1. String path = request.getSession().getServletContext().getRealPath("/");  
  2. Properties p = new Properties();  
  3. p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "");  
  4. p.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");  
  5. p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");  
  6. p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");  
  7. try {  
  8.     Velocity.init(p);  
  9.     Template template = Velocity.getTemplate(path  
  10.             + "WEB-INF\\velocity\\模版文件名");  
  11.     VelocityContext context = new VelocityContext();  
  12.     context.put("insurance", insurance);  
  13.     FileOutputStream fos = new FileOutputStream(path + "aaa" + ".html");  
  14.     BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(  
  15.             fos, "UTF-8"));//设置写入的文件编码,解决中文问题  
  16.     template.merge(context, writer);  
  17.     writer.close();  
  18. catch (Exception e) {  
  19.     e.printStackTrace();  
  20. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值