静态页面技术+Feign+springcloud

文件放入的文件夹同https://my.oschina.net/u/4083694/blog/3082389差不多

1.发送端服务的接口

@RestController
public class StaticPageController {
    //必须是post请求,get请求会报错
    @PostMapping("/getStaticPage")
    public AjaxResult getStaticPage(@RequestBody Map<String,Object> map){
        Object model = map.get("model");
        String templatePath = (String) map.get("templatePath");//模板路径
        String targetPath = (String) map.get("targetPath");//生成后的路径
        try {
            VelocityUtils.staticByTemplate(model,templatePath,targetPath);
            return AjaxResult.myAjaxResult().setSuccess(true).setMessage("保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myAjaxResult().setSuccess(false).setMessage("保存失败!");
        }
    }
}

2.工具类(导入velocity包)直接拷贝使用即可

public class VelocityUtils {
   private static Properties p = new Properties();
   static {
      p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "");
      p.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
      p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
      p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
   }
   
   /**
    * 返回通过模板,将model中的数据替换后的内容
    * @param model
    * @param templateFilePathAndName
    * @return
    */
   public static String getContentByTemplate(Object model, String templateFilePathAndName){
      try {
         Velocity.init(p);
         Template template = Velocity.getTemplate(templateFilePathAndName);
         VelocityContext context = new VelocityContext();
         context.put("model", model);
         StringWriter writer = new StringWriter();
         template.merge(context, writer);
         String retContent = writer.toString();
         writer.close();
         return retContent;
      } catch (Exception e) {
         e.printStackTrace();
      }
      return "";
   }

   /**
    * 根据模板,静态化model到指定的文件 模板文件中通过访问model来访问设置的内容
    * 
    * @param model
    *            数据对象
    * @param templateFilePathAndName
    *            模板文件的物理路径
    * @param targetFilePathAndName
    *            目标输出文件的物理路径
    */
   public static void staticByTemplate(Object model, String templateFilePathAndName, String targetFilePathAndName) {
      try {
         Velocity.init(p);
         Template template = Velocity.getTemplate(templateFilePathAndName);
         
         VelocityContext context = new VelocityContext();
         //模板中定义了一个数据,key是model
         context.put("model", model);
         FileOutputStream fos = new FileOutputStream(targetFilePathAndName);
         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));// 设置写入的文件编码,解决中文问题
         template.merge(context, writer);
         writer.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   /**
    * 静态化内容content到指定的文件
    * 
    * @param content
    * @param targetFilePathAndName
    */
   public static void staticBySimple(Object content, String targetFilePathAndName) {
      VelocityEngine ve = new VelocityEngine();
      ve.init(p);
      String template = "${content}";
      VelocityContext context = new VelocityContext();
      context.put("content", content);
      StringWriter writer = new StringWriter();
      ve.evaluate(context, writer, "", template);
      try {
         FileWriter fileWriter = new FileWriter(new File(targetFilePathAndName));
         fileWriter.write(writer.toString());
         fileWriter.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

}

3.模板的vm文件大概样子

fc3be079115c0dfd0c8e08bd99b40613bfe.jpg

转载于:https://my.oschina.net/u/4083694/blog/3082423

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值