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);
}
}
}
- velocity模板引擎处理
- velocity模板引擎简介
- velocity 模板引擎
- Velocity模板引擎
- 【工具】模板引擎 Velocity
- Velocity模板引擎入门
- 模板引擎 Velocity
- Velocity 模板引擎介绍
- 模板引擎 Velocity
- Java模板引擎---Velocity
- Velocity模板引擎
- Velocity 模板引擎介绍
- velocity 模板引擎
- Velocity 模板引擎介绍
- Velocity模板引擎
- velocity模板引擎学习
- Velocity模板引擎
- Velocity模板引擎语法
- 欢迎使用CSDN-markdown编辑器
- Ubuntu 格式化硬盘
- 阶乘
- 区间信息的维护和查询系列算法-树状数组
- qtcreator项目目录管理
- velocity模板引擎处理
- poj 3073 Spam(注意转义字符‘\’+dp)
- Greenplum闰秒故障的分析解决
- Vim Tutor
- ctags的简单使用
- java导入导出Excel poi跟jxl
- mac 下使用 minicom 进行串口调试
- Android仿qq下拉刷新及向左滑动列表----PullToRefresh, SwipeMenuListView开源项目整合
- 用GDB调试程序(二)