1、将freemarker的jar包添加到项目中
2、创建Action类
我们可以在需要显示该模板的页面中调用该方法,就会显示出相应的模板内容:
2、创建Action类
//此处的Configuration是freemarker包中的Configuration因为我们在开发中会用到
//各种框架例如hibernate,都会有Configuration,所以别引入错了
private static Configuration cfg = new Configuration();
static {
//配置freemarker从什么地方加载模板
cfg.setTemplateLoader(new ClassTemplateLoader(DocumentAction.class, "templates"));
//忽略异常
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
}
//获取动态的表单
public String dynaForm(int workFlowId) {
try {
//加载与该流程定义相关的流程表单对象
FlowForm form = this.flowFormService.searchFlowFormByWorkFlowId(workFlowId);
if(form == null) {
return null;
}
//得到该流程表单所应用的模板
Template template = cfg.getTemplate(form.getTemplate());
Map root = new HashMap();
//将查询出来的form对象通过map放到模板中,在模板的定义中会使用到,调用的时候会填充好数据显示出来
root.put("form", form);
Writer out = new StringWriter();
//将根据模板生成页面数据显示到相应的页面上
template.process(root, out);
return out.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
我们可以在需要显示该模板的页面中调用该方法,就会显示出相应的模板内容:
<s:property value="dynaForm(workFlowId)" escape="false" />