使用Freemarker和Struts2做动态表单

   由于项目中需要用到一个可定制的动态表单功能,管理员可以根据需要定制定出想要的表单,参考了一些视频和网上资料,自己做了一个动态电子表单模块。
     //FreemarkerManage 的初始配置
    public class FreemarkerManage {

	private static String ENCODING="UTF-8";
	private static Configuration cfg=new Configuration();
	static{
		//定义模板的位置
		cfg.setTemplateLoader(new ClassTemplateLoader(FreemarkerManage.class,"templates"));
		cfg.setObjectWrapper(new DefaultObjectWrapper());
		//设置异常
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
		cfg.setEncoding(Locale.CHINA, ENCODING);
	}
	public static Configuration getConfiguration(){
		return cfg;
	}

}


@Component
public class DynaFormFunction {
	private static String defaultTemplate = "document_form.ftl";
	private static String showTemplate = "showform.ftl";
	private static String initForm = "initForm.ftl";
	private static FlowFormManageService flowFormManageService;
	private static FormInstanceWebService formInstanceWebService;
	private static ReportJobWebService reportJobWebService;
	/***
	 * 初始化表单信息,将数据放到模板中
	 * @param workflowId
	 * @return
	 */
	public static String form(long workflowId) {
        FlowForm flowForm = flowFormManageService.findFlowFormById(workflowId);
		if (null == flowForm) {
			return null;
		}
		/**
		 * 加载freemarker的配置
		 */
	Configuration configuration = FreemarkerManage.getConfiguration();
		try {
			/**
			 * 加载表单模板文件
			 */
		Template template = configuration.getTemplate(document_form);
		Writer writer = new StringWriter();
		Map root = new HashMap();
		root.put("form", flowForm);
			/**
			 * 将模板与数据进行相结合
			 */
			template.process(root, writer);
			return writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 显示动态表单的值
	 * @param formInstanceId
	 * @return
	 */
	public static String showForm(long formInstanceId) {
           try {
	FormInstanceDTO formInstanceDTO = formInstanceWebService
					.findFormInstanceById(formInstanceId);
			if (null == formInstanceDTO) {
				return null;
			}
			/**
			 * 加载freemarker的配置
			 */
	Configuration configuration = FreemarkerManage.getConfiguration();
			/**
			 * 获取动态表单的实例数据
			 */
	Map<Long, Object> dataInstance = formInstanceDTO.getInstanceData();
	Map<String, Object> newDataInstance = reportJobWebService
					.findFlowFormData(dataInstance);
			/**
			 * 根据配置加载模板
			 */
	Template template = configuration.getTemplate(showTemplate);
			Writer writer = new StringWriter();
			Map root = new HashMap();
			root.put("showForm", newDataInstance);
			/**
			 * 将模板与数据进行结合
			 */
			template.process(root, writer);
			return writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
        }
}
模板文件的编写
(1).initForm.ftl的编写
<table>
	<#assign i=0>
	<#assign dataValue="">
	<#list form.formFields as field>
		<#assign i=i+1>
		<#if field_index % 2 = 0>
			<tr>
				</#if>
					<td colspan="2" valign="top">
						<span class="form-field"><span class="important-field-star">*</span>${field.fieldLable}:</span>
						<#include "${field.input.key}">
					</td>
				<#if i=2 || !field_has_next >
					<#assign i=0>
			</tr>
		</#if>
	</#list>
</table>

/****
*自定义一个tld文件放在WEB-INF下,文件名称叫my.tld
*/
<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  version="2.0">
    
  <tlib-version>1.0</tlib-version>
  <short-name>my</short-name>
  <uri>http://www.gosophia.com/oa/functions</uri>
  <function>
    <name>form</name>
    <function-class>com.gosophia.reportData.action.DynaFormFunction</function-class>
    <function-signature>java.lang.String form(long)</function-signature>
  </function>
  <function>
     <name>showForm</name>
    <function-class>com.gosophia.reportData.action.DynaFormFunction</function-class>
    <function-signature>java.lang.String showForm(long)</function-signature>
  </function>
</taglib>
/**
**在web.xml文件进行配置
*/
	<jsp-config>
		<taglib>
		<taglib-uri>http://www.gosophia.com/myfunctions</taglib-uri>
		<taglib-location>/WEB-INF/my.tld</taglib-location>
		</taglib>
	</jsp-config>

在jsp页面显示,并且引进tld文件
 /**
  *
  *初始化电子表单页面
  */
//先在jsp页面引进自己的标签
<%@taglib prefix="my" uri="http://www.gosophia.com/oa/functions"%>
//在jsp直接引入就可以
   ${my:form(flowForm.formId)}

  /**
   *动态显示电子表单内容
   *
   */
//先在jsp页面引进自己的标签
<%@taglib prefix="my" uri="http://www.gosophia.com/oa/functions"%>
//显示动态表单中数据
${my:showForm(approvalReportJobDto.reportJobDto.formInstanceId)}

    由于这个模块是在整个项目当中的,没能提供出相应的源代码。由于时间原因自己没能做出一个例子。
    上面的模板文件没有定义全,如果想要真正使用的话,还的定义出其他模板文件。
    如图: 使用Freemarker和Struts2做动态表单
    如果上面有不对的或不清楚的地方,请大家留言,或加我msn通知我      zhaoyanfangmsn@hotmail.com

你可能感兴趣的:(Web,freemarker,jsp,配置管理,项目管理)