velocity模板引擎处理

来源:互联网 发布:mac打开xlsx 不能编辑 编辑:程序博客网 时间:2024/05/17 04:13

velocity模板调用(velocity-1.6.2.jar)

public class EndorseConstants {
public static boolean isNotVelocityInit=true;
 /**全局初始化velocity投保保费计算和批改比较生成批文的问题,动态模板只能初始化一次*/
 public synchronized static void initVmPro() throws BusinessServiceException{
  try {
   Properties p = new Properties();
   p.setProperty("resource.loader", "string");
   p.setProperty("string.resource.loader.class",
     "org.apache.velocity.runtime.resource.loader.StringResourceLoader");
   p.setProperty("string.resource.loader.repository.class",
   "com.isoftstone.pcis.policy.app.edrrsn.bo.EndorseWordingTemplateRepository"); 
   try {
    Velocity.init(p);
   } catch (Exception e) {
    Velocity.init(p);
   }
   isNotVelocityInit=false;
  } catch (Exception e) {
   e.printStackTrace();
   logger.error("模板初始化属性出错!");
   throw new BusinessServiceException("模板初始化属性出错", e);
  }
 }
}

/**车险批改调用解析velocity模板方法*/
public class EndorseWordingTemplateRepository implements StringResourceRepository {

    public String getEncoding() {
        // TODO Auto-generated method stub
        return "UTF-8";
    }

    public StringResource getStringResource(String name) {
        // TODO Auto-generated method stub
        String nameSects[] = name.split(",");
        String edrType = nameSects[0];
        String prodNo = nameSects[1];
        String reasonCode = nameSects[2];
        if (StringUtils.isEmpty(edrType)) {
            throw new ResourceNotFoundException("查询批改模板失败:批改类型为空");
        }
        if (StringUtils.isEmpty(edrType)) {
            throw new ResourceNotFoundException("查询批改模板失败:批改原因代码为空");
        }
        if (StringUtils.isEmpty(edrType)) {
            throw new ResourceNotFoundException("查询批改模板失败:产品代码为空");
        }

        try {
            InputStream ins = null;
            IEndorseConfService endorseConfService = (EndorseConfService) SpringUtils.getSpringBean("endorseConfService");
            String str = endorseConfService.getEndorseWordingTemplateList(edrType, prodNo, reasonCode);
   //str字符串格式
   //兹经双方协定,本保险单${draftsman.PolicyNo}自${draftsman.StartDate}增加如下保险责任:
            //${draftsman.draft("Cvrg","*")}${draftsman.draft("Mechine","*")},鉴于上述情况,
            //本公司向投保人收取(退还)保费一笔,${draftsman.descChargeTotal()}。其他条件不变,特此批注。

            return new StringResource(str, this.getEncoding());
        } catch (BusinessServiceException e) {
            throw new ResourceNotFoundException("查询批改模板失败:批改类型" + edrType + "原因代码" + reasonCode
                    + "产品代码" + prodNo);
        }
    }

    public void putStringResource(String arg0, String arg1, String arg2) {
        // TODO Auto-generated method stub

    }

    public void putStringResource(String arg0, String arg1) {
        // TODO Auto-generated method stub

    }

    public void removeStringResource(String arg0) {
        // TODO Auto-generated method stub

    }

    public void setEncoding(String arg0) {
        // TODO Auto-generated method stub

    }

}
 
   public class EndorseWordingHelper {
  private static final Logger logger = Logger.getLogger(EndorseWordingHelper.class);
    /**
   * velocity 生成批文
   */
  public static String genWording(IWordingDraftsman draftsman) throws BusinessServiceException {
   logger.debug("批文生成开始");
   if (!draftsman.hasDiff()) {
    return SpringUtils.getMessage("policy.edr.wording.nodiff");
   }

   try {
    if(EndorseConstants.isNotVelocityInit){//如果初始化(Velocity.init)过,则不能重新初始化模板
     EndorseConstants.initVmPro();
    }
    String reasonCode = null;//复驶特殊处理字段
    VelocityContext velocityContext = new VelocityContext();
    String reasonOrBundleCode = reasonCode == null ? draftsman.getReasonOrBundleCode() : reasonCode;
    Template template = Velocity.getTemplate(draftsman.getEdrType() + ","
      + draftsman.getProdNo() + "," + reasonOrBundleCode, "UTF-8");
    velocityContext.put("draftsman", draftsman);
    StringWriter sw = new StringWriter();
    template.merge(velocityContext, sw);
    logger.debug("批文内容\n" + sw.toString());
    logger.debug("批文生成正常结束");

    return sw.toString();
   } catch (Exception e) {
    e.printStackTrace();
    logger.error("模板初始化属性出错!");
    throw new BusinessServiceException("模板初始化属性出错", e);
   }
  }
 }

 

    public class QuickReadVelocityUtils {
    private static Logger logger = Logger.getLogger(QuickReadVelocityUtils.class);
    //**************************************************************************
  /**
   * 转换模板文件
   */
  //**************************************************************************
  public static String readJavaVelocityStr(PolicyApplication app,String template) throws Exception{
   try{
    if(EndorseConstants.isNotVelocityInit){//如果初始化(Velocity.init)过,则不能重新初始化模板
     EndorseConstants.initVmPro();
    }
    VelocityContext velocityContext = new VelocityContext();
    
    //将业务类PolicyApplication注入velocity
    velocityContext.put("app", app);
    QuickChangeHelperUtils helper=new QuickChangeHelperUtils();
    helper.setApp(app);
    velocityContext.put("helper", helper);
    StringWriter sw = new StringWriter();
    
    Velocity.evaluate( velocityContext,sw, "mystring",template); 
    String result = sw.toString();
    
    return result;
   }catch (Exception e) {
    e.printStackTrace();
    logger.error("固定特约生成异常结束");
    throw new BusinessServiceException("生成固定特约时出错", e);
   }
  }
 }

0 0
原创粉丝点击