freemarker模板引擎使用

定义模板文件spareinfo.ftl

<div id="sc" style=" width: 100%; border-bottom:1px solid #ddd;border-left:1px solid #ddd;border-right:1px solid #ddd" class="info">
   <table width="100%" style="line-height: 40px; border-collapse: collapse; border-width: 0px; border-style: hidden;" border="1" cellspacing="0" cellpadding="0">
        <tr>
            <td style="width: 20%;" class="td1">备件信息</td>
            <td>
                <table width="100%" style="border-collapse: collapse; border-width: 0px; border-style: hidden;" border="1" cellspacing="0" cellpadding="0"  id="jk">
                    <tr>
                        <td colspan="5">
                            <div id="o_unitD" style="overflow: hidden;">
								<!-- 是否原物项 -->
                                <div class="flex" id="sfywx" style="margin-left: 10px;">
                                    <span>是否有原物项</span>
                                    <label><input id="sfywx1" name="sfywx"   type="radio"  value="1" onchange="shifou()">是</label>
                                    <label><input checked="checked" id="sfywx2" name="sfywx" type="radio"  value="2" onchange="shifou()">否</label>
									<input id="templateId" name="templateId" value="${templateId}" style="display: none;">
                                </div>
                            </div>
						</td>
                    </tr>
                    <tr>
						<!-- 【bigClass】大类,判断大类是否有值来控制列数的展示 【bigClass??】用于判断是否空值-->
                        <#if bigClass??  &&  bigClass != ""  >
                        <td style="width: 25%" class="td1" colspan="2">信息項目</td>
                        <#else>
                        <td style="width: 25%" class="td1" >信息項目</td>
                        </#if>
                        <td style="width: 25%" class="td1">原始物项<input id="sfywx1" class="sfywxAll" name="sfywx" type="button"  value="读取SAP" onclick="fetchSAP(1)" /></td>
						<!-- 【ywx】前台sj用【document.getElementsByClassName("ywx")】来根据name获取 table中的原物项 -->
                        <td style="width: 25%; display:none;" class="td1 ywx">原物项<input id="sfywx1" class="sfywxAll" name="sfywx" type="button"  value="读取SAP" onclick="fetchSAP(3)"</td>
                        <td style="width: 25%" class="td1 twx">替代物项<input id="sfywx1" class="sfywxAll" name="sfywx" type="button"  value="读取SAP" onclick="fetchSAP(4)" /></td>
                    </tr>
					<!-- 【list】用于循环展示,【mblist】后台返回的参数值,【sort_by】排序,【sortNo】序号字段 -->
                    <#list mblist?sort_by("sortNo") as record>
                        <tr>
							<!-- 【bigClass】大类,判断大类是否有值来控制列数的展示 【bigClass??】用于判断是否空值 -->
                            <#if record.bigClass??  &&  record.bigClass != ""  >
							<!-- 【apps】用于前台js 获取表示 ,【appid】用于获取主键ID-->
                            <td style="width: 10%" class="td1  apps" appid="${record.infoId}">${record.bigClass} 
								<input id="bigClass_${record.infoId}" name="bigClass_${record.infoId}" value="${record.bigClass}" style="display: none;">
							</td>
                            <td style="width: 15%" class="td1">${record.smallClass}
								<input id="smallClass_${record.infoId}" name="smallClass_${record.infoId}" value="${record.smallClass}" style="display: none;">
								<input id="infoId_${record.infoId}" name="infoId_${record.infoId}" value="${record.infoId}" style="display: none;">
								<input id="sortNo_${record.infoId}" name="sortNo_${record.infoId}" value="${record.sortNo}" style="display: none;">
								<input id="spareId_${record.infoId}" name="spareId_${record.infoId}" value="${record.spareId!}" style="display: none;">
							</td>
                            <#else>
                            <td class="td1 apps" appid="${record.infoId}">${record.smallClass}
								<input id="smallClass_${record.infoId}" name="smallClass_${record.infoId}" value="${record.smallClass}"style="display: none;" >
								<input id="infoId_${record.infoId}" name="infoId_${record.infoId}" value="${record.infoId}" style="display: none;">
								<input id="sortNo_${record.infoId}" name="sortNo_${record.infoId}" value="${record.sortNo}" style="display: none;">
								<input id="spareId_${record.infoId}" name="spareId_${record.infoId}" value="${record.spareId!}" style="display: none;">
							</td>
                            </#if>
                           
                            <td class="td1" appid="${record.infoId}">
								<input id="initialItemNo_${record.infoId}" name="initialItemNo_${record.infoId}" value="${record.initialItemNo!}" class="nui-textbox fen" style="width: 75%; border: 1px solid #ddd;border-radius: 3px;height: 35px;margin: 10px 0; padding-left: 20px;" onchange="itemBackGround()">
                            </td>
							<!-- 根据是否原物项展示,默认隐藏 -->
                            <td style="display:none;" class="td1 ywx column" appid="${record.infoId}">
								<input id="orginItem_${record.infoId}" name="orginItem_${record.infoId}" value="${record.orginItem!}" class="nui-textbox" style="width: 75%;border: 1px solid #ddd;border-radius: 3px;height: 35px;margin: 10px 0; padding-left: 20px;" >
                            </td>
                            <td class="td1 twx column" appid="${record.infoId}">
								<input id="replaceItemNo_${record.infoId}" name="replaceItemNo_${record.infoId}" value="${record.replaceItemNo!}" class="nui-textbox" style="width: 75%;border: 1px solid #ddd;border-radius: 3px;height: 35px;margin: 10px 0; padding-left: 20px;" onchange="itemBackGround()">
                            </td>
                        </tr>
                    </#list>
                </table>
            </td>
        </tr>
    </table>
 </div>

获取模板

	/**
	* 获取模板文件
	* @param name
	* @return
	*/
	public Template getTemplate(String name){
	Template template = null;
	String classpath = this.getClass().getResource("/").getPath().replaceFirst("/", "");
	String webappRoot = classpath.replaceAll("WEB-INF/classes/", "ftl/");
	Configuration cfg = new Configuration();
	try {
		cfg.setDirectoryForTemplateLoading(new File(webappRoot));
		template = cfg.getTemplate(name);
	} catch (IOException e) {
		// TODO 自动生成的 catch 块
		e.printStackTrace();
	}
		return template;
	}

调用模板

try {
	// 获取一个模板对象
	Template t = getTemplate("spareinfo.ftl")
	// 执行插值,并输出到指定的输出流中
	Writer writer = new StringWriter();
	Map<String, Object> map = new HashMap<String, Object>();
	map.put("mblist", list);// list变了前台数据
	map.put("bigClass", bigClass);// 用于判断是否合并单元格
	map.put("templateId", templateId);//技术信息类型ID
	t.process(map, writer);
	s = writer.toString();
	System.out.println(s);
} catch (Exception e) {
	// TODO: handle exception
	e.printStackTrace();
}

猜你喜欢

转载自blog.csdn.net/qq_29988051/article/details/89085547
今日推荐