【FreeMarker】【模板文件FTL】模板指令/标签 <#> <@>

本文深入探讨了如何使用FreeMarker的FTL标签来调用directives指令,涵盖了开始标签、结束标签的使用方式,以及指令的分类(预定义与用户自定义)。详细解释了参数的格式和模板中指令的执行流程,并强调了正确使用FTL标签的重要性,避免错误执行和提升模板的灵活性。

指令/标签

使用FTL标签来调用 directives 指令。

标签分为:

  • 开始标签 —— <#directivename parameters>
  • 结束标签 —— </#directivename>

除了标签以 # 开头外,其他都和HTML, XML语法很相似。如果标签没有嵌套内容,可以只使用开始标签。

parameters的格式由 directivename 来决定。


指令分为 预定义指令 和 用户自定义指令

用户自定义的指令使用 @ 来代替 #。而且如果用户自定义指令没有嵌套内容,那么必须使用 <@mydirective parameters />形式。


如果尝试使用一个不存在的指令,FreeMarker就会拒绝执行模板,同时抛出错误信息。

FreeMarker会忽略FTL标签中的多余空白标记。

请分析下面代码:<#setting datetime_format="yyyy-MM-dd"/> <#import "/decorators/includes/macros.ftl" as m> <#assign ww = JspTaglibs["/WEB-INF/webwork.tld"] /> <#include "/template/includes/actionerrors.ftl"> <link type="text/css" rel="StyleSheet" href="../css/sortabletable.css"/> <div id="breadcrumbs"> <a href="${req.contextPath}/index.action">Home</a> > Unlock List </div> <center><font color="red" size="2"><b>MES Product Id version mismatch with your RunCard! <br>So system lock RunCard and waiting for your confirmation. <br>You can unlock the RunCard and then revise the RunCard.</b></font></center> <div class="app"> <font color="red">${message?if_exists}</font> <h3>Unlock List</h3> <table border="0" cellspacing="2" cellpadding="3" width="100%" id="unlockList"> <thead> <tr> <th>Case No</th> <th>Applicant</th> <th>Lot Id</th> <th>Product Id</th> <th>Status</th> <th>Remark</th> <th>Action</th> </tr> </thead> <tbody> <#if lockRuncard?has_content> <#list lockRuncard as lock> <tr class="b"> <@ww.form name="&#39;lockForm${lock_index}&#39;" action="&#39;unlockRuncard&#39;" namespace="&#39;/user&#39;" validate="&#39;true&#39;" method="&#39;post&#39;" theme="&#39;simple&#39;"> <@ww.hidden name="&#39;requestFormId&#39;" value="&#39;${lock.id?if_exists}&#39;"/> <td><a href="../user/viewRequestForm.action?requestFormId=${lock.id}">${lock.caseNo?default("Not Assigned")?if_exists}</a></td> <td><@m.directoryLink lock.applicant?if_exists/></td> <td>${lock.lotId?if_exists}</td> <td>${lock.prodId?if_exists}</td> <td>${lock.status?if_exists}</td> <td width="10%"><@ww.textarea name="&#39;remark&#39;" id="remark${lock_index}" cols="35" rows="3" theme="&#39;simple&#39;" required="true"/></td> <td> <input type="button" onclick="checkRemark(${lock_index})" value=" Pass "> <#--| <input type="button" onclick="javascript:if(confirm(&#39;Are you sure?&#39;))location.href=&#39;withdrawLockRuncard.action?requestFormId=${lock.id}&#39;" value="Withdraw"--> </td> </@ww.form> </tr> </#list> <script type="text/javascript"> var st0 = new SortableTable(document.getElementById("unlockList"), ["String", "String" ,"String" ,"String"]); function checkRemark(index){ var reg=/\s/g; //var realValue=$F(&#39;remark&#39;).replace(reg,""); var realValue=$F(&#39;remark&#39;+index).replace(reg,""); if(realValue==""){ alert("Please input the remark !"); return false; } if(!confirm(&#39;Product Id version mismatch! You confirm ok and need unlock RunCard?&#39;)) return false; $(&#39;lockForm&#39;+index).submit(); } </script> <#else> <tr colspan="6"> <td> No related result.</td> </tr> </#if> </tbody> </table>
09-09
请分析下面代码:<#import "/decorators/includes/macros.ftl" as m> <#assign ww = JspTaglibs["/WEB-INF/webwork.tld"] /> <#include "/template/includes/actionerrors.ftl"> <#assign useStepNo=false> <#if requestForm.stepForms?exists> <#list requestForm.stepForms as stepForm> <#if stepForm.stepNo?exists> <#assign useStepNo=true> </#if> </#list> </#if> <script> document.onkeydown = function() { if(event.keyCode==116) { event.keyCode=0; event.returnValue = false; } if ((window.event.altKey)&&(window.event.keyCode==115)){ //屏蔽Alt+F4 event.keyCode=0; event.returnValue=false; alert("Please do not exit like this again. This case maybe have some problem cause of the action.Please check it.\r\n 请下次不要再这样退出。由于你的这个动作,这个单子可能已经产生了错误,请检查。"); return false; } if ((event.ctrlKey)&&(event.keyCode==87)){ //屏蔽 Ctrl+n event.keyCode=0; event.returnValue=false; return false; } } //document.oncontextmenu = function() {event.returnValue = false;} function continueRuncard(){ var date = new Date(); var minute=date.getMinutes(); if(parseInt(minute)>=0 && parseInt(minute)<=5) { alert("整点系统同步中.\n请稍后再试!"); return false; } var compareResultComment=document.getElementById("compareResultComment"); if (compareResultComment!=null){ confineStringLength(compareResultComment,500); if(compareResultComment.value==""){ return false; } } var form = document.forms[&#39;&#39;]; form.action="continueRuncard.action"; form.submit(); } function cancelRuncard(){ var date = new Date(); var minute=date.getMinutes(); if(parseInt(minute)>=0 && parseInt(minute)<=5) { alert("整点系统同步中.\n请稍后再试!"); return false; } var form = document.forms[&#39;submitRequestFormForm&#39;]; form.action="cancelRuncard.action"; form.submit(); } function compareWithMes(){ <#if Rework> alert("Because RunCard&#39;s category is rework. \r\n System will not execute this function!"); return false; </#if> var form = document.forms[&#39;submitRequestFormForm&#39;]; if(!confirm("Do you want to create the comparison with MES document?\n你是否需要E-RC与MES做对比?\n\n确定 = E-RC对比MES(此动作可能需要花费几分钟时间,请耐心等待)\n\n取消 = E-RC不比对MES")){ return false; } form.action="eRuncardRule.action"; form.submit(); } function submitRunCard(){ var compareResultComment=document.getElementById("compareResultComment"); if (compareResultComment!=null){ confineStringLength(compareResultComment,500); if(compareResultComment.value==""){ return false; } } var date = new Date(); var minute=date.getMinutes(); if(parseInt(minute)>=0 && parseInt(minute)<=5) { alert("整点系统同步中.\n请稍后再试!"); return false; } disableButton(); var f=document.submitRequestFormForm; f.action = "submitRequestForm.action"; var xmlHttp = XmlHttp.create(); var async = false; xmlHttp.open("GET", "checkStepNoExistInFecp.action?lotId=${requestForm.lotId?if_exists}&holdStepNo=${requestForm.holdStepNo?if_exists}", async); xmlHttp.send(null); if(xmlHttp.responseText != null && xmlHttp.responseText == &#39;<success/>&#39;){ if (!confirm("你确认是否需要在这步做STR/MSTR and Link STR/MSTR \nNumber? If yes, 请确认E-runcard condition exactly \n match with FECP/STR, MSTR system\n\n确定 = 确认 Link STR/MSTR Number, submit E-runcard \n\n取消 = 修改 E-runcard")){ f.action = "viewUpdateRequestForm.action"; //f.submit(); } f.submit(); }else if(xmlHttp.responseXML != null && xmlHttp.responseXML.getElementsByTagName("success").length > 0){ if (!confirm("你确认是否需要在这步做STR/MSTR and Link STR/MSTR \nNumber? If yes, 请确认E-runcard condition exactly \n match with FECP/STR, MSTR system\n\n确定 = 确认 Link STR/MSTR Number, submit E-runcard \n\n取消 = 修改 E-runcard")){ f.action = "viewUpdateRequestForm.action"; //f.submit(); } f.submit(); }else{ f.submit(); } } </script> <@ww.form name="&#39;submitRequestFormForm&#39;" namespace="&#39;/user&#39;" validate="&#39;true&#39;" method="&#39;post&#39;" theme="&#39;simple&#39;"> <@ww.hidden name="&#39;requestFormId&#39;" value="${requestForm.id}"/> <@ww.token name="submitToken"/> <div class="app"> <h3>View e-Runcard Request Form</h3> <table border="0" cellspacing="1" cellpadding="8" width="100%"> <tr class="b"> <td>RunCard No.:</td> <td>${requestForm.caseNo?if_exists}</td> <td>Status:</td> <td><font color="red">${requestForm.status?if_exists}</font></td> </tr> <tr class="b"> <td>Applicant:</td> <td><@m.directoryLink requestForm.applicant?if_exists/> </td> <td>Organization:</td> <td>${requestForm.applicantOrg?if_exists}</td> </tr> <tr class="b"> <td>Submit Date:</td> <td>${requestForm.submitDate?if_exists}</td> <td>Effective Date:</td> <td>${requestForm.effectiveDate?if_exists}</td> </tr> <#include "viewIncludeLotInfo.ftl"/> <#include "viewIncludeSignInfo.ftl"/> <#include "viewIncludeErcHeaderInfo.ftl"/> <#include "viewIncludeErcCompleteInfo.ftl"/> <#switch requestForm.formType> <#case "Auto Reposition Step"> <#break> <#default> <table border="0" cellspacing="1" cellpadding="8" width="100%"> <tr> <th colspan="4">RunCard Steps Information</th> </tr> <#assign showWGAction = true > <#include "stepForm/includeStepForm.ftl"/> </table> </#switch> </table> <hr/> <#if requestForm.match?default("")=="N"> <table border="0" cellspacing="1" cellpadding="8" width="100%"> <tr class="b"> <td>Compare Result Comment <BR>请输入原因(Only 500 characters)</td> <td><@ww.textarea name="&#39;requestForm.compareResultComment&#39;" id="compareResultComment" cols="80" rows="5" theme="&#39;simple&#39;"/></td> </tr> </table> </#if> <table width="100%"> <tr> <td class="center"> <#if requestForm.formType="Normal"> <input type="button" onclick="compareWithMes()" value="Compare"/>     </#if> <#if requestForm.haveQtime?exists> <#if requestForm.haveQtime=="Y"> <#if requestForm.formType!="Scan Defect"> <#if requestForm.formType="Normal" && useStepNo && requestForm.match=="No Comparison" && !Rework> <input type="button" class="btn" value="Continue Runcard" onclick="alert(&#39;please compare runcard first.&#39;);"/>     <#else> <input type="button" class="btn" value="Continue Runcard" onclick="continueRuncard()"/>     </#if> </#if> <input type="button" class="btn" value="Cancel Runcard" onclick="cancelRuncard()"/>     </#if> <#else> <#if requestForm.formType="Normal" && useStepNo && requestForm.match=="No Comparison" && !Rework> <input type="button" class="btn" value="Submit" onclick="alert(&#39;please compare runcard first.&#39;);"/>     <#else> <input type="button" class="btn" value="Submit" onclick="submitRunCard()"/>     </#if> <input type="button" class="btn" value="Back" onclick="document.location.href=&#39;${req.contextPath}/user/viewUpdateRequestForm.action?requestFormId=${requestForm.id}&#39;"/> </#if> </td> </tr> </table> <div> </@ww.form>
09-09
详细解释这个模板的渲染过程<#-- 独立计算全局最大三日平均变化量的观测点 --> <#if maxDatePeriod??> <#assign globalMaxThreeDayAverage = -999999> <#assign globalMaxThreeDayPoint = {}> <#assign globalMaxKey = ""> <#list maxDatePeriod?keys as monitorKey> <#if maxDatePeriod[monitorKey]?? && (maxDatePeriod[monitorKey]?size > 2)> <#assign dataList = maxDatePeriod[monitorKey]> <#-- dataList[2] 是三日平均值数据 --> <#if dataList[2]?? > <#assign threeDayAveragePoint = dataList[2]> <#if (threeDayAveragePoint.speedResult??) && (threeDayAveragePoint.speedResult?has_content) && (threeDayAveragePoint.speedResult?is_number) && (threeDayAveragePoint.speedResult?number > globalMaxThreeDayAverage)> <#assign globalMaxThreeDayAverage = threeDayAveragePoint.speedResult?number> <#assign globalMaxThreeDayPoint = threeDayAveragePoint> <#assign globalMaxKey = monitorKey> </#if> </#if> </#if> </#list> <#-- 独立渲染全局最大三日平均值信息 --> <w:p> <w:pPr> <w:spacing w:line="360" w:lineRule="auto"/> <w:ind w:firstLine="480"/> <w:rPr> <w:rFonts w:hint="default" w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/> <w:sz w:val="21"/> <w:szCs w:val="21"/> <w:lang w:val="en-US" w:eastAsia="zh-CN"/> </w:rPr> </w:pPr> <w:r> <w:rPr> <w:rFonts w:hint="default" w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/> <w:sz w:val="21"/> <w:szCs w:val="21"/> <w:lang w:eastAsia="zh-CN"/> </w:rPr> <#-- 渲染全局最大三日平均值信息 --> <#if globalMaxThreeDayPoint?has_content> <#if globalMaxThreeDayPoint.monitorModelId?? && globalMaxThreeDayPoint.monitorModelId == "3"> <#-- 测斜 --> <w:t>其中<#if globalMaxKey??>${globalMaxKey}<#else>未知类型</#if>监测点本次三日平均变化速率最大,<#if globalMaxThreeDayPoint.parentName??>${globalMaxThreeDayPoint.parentName}<#else>未知点</#if>(<#if globalMaxThreeDayPoint.pointName??>${globalMaxThreeDayPoint.pointName}<#else>未知</#if>m)观测点三日平均变化速率最大,三日平均变化速率 <#if globalMaxThreeDayPoint.speedResult??>${globalMaxThreeDayPoint.speedResult}<#else>0</#if><#if globalMaxThreeDayPoint.changeUnit??>${globalMaxThreeDayPoint.changeUnit}<#else>mm/d</#if>。</w:t> <#elseif globalMaxThreeDayPoint.monitorModelId?? && globalMaxThreeDayPoint.monitorModelId == "5"> <#-- 支撑轴力 --> <w:t>其中<#if globalMaxKey??>${globalMaxKey}<#else>未知类型</#if>监测点本次三日平均变化速率最大,<#if globalMaxThreeDayPoint.parentName??>${globalMaxThreeDayPoint.parentName}<#else>未知点</#if>观测点三日平均变化速率最大,三日平均变化速率 <#if globalMaxThreeDayPoint.speedResult??>${globalMaxThreeDayPoint.speedResult}<#else>0</#if><#if globalMaxThreeDayPoint.changeUnit??>${globalMaxThreeDayPoint.changeUnit}<#else>mm/d</#if>。</w:t> <#else> <#-- 其他类型 --> <w:t>其中<#if globalMaxKey??>${globalMaxKey}<#else>未知类型</#if>监测点本次三日平均变化速率最大,<#if globalMaxThreeDayPoint.pointName??>${globalMaxThreeDayPoint.pointName}<#else>未知点</#if>观测点三日平均变化速率最大,三日平均变化速率 <#if globalMaxThreeDayPoint.speedResult??>${globalMaxThreeDayPoint.speedResult}<#else>0</#if><#if globalMaxThreeDayPoint.changeUnit??>${globalMaxThreeDayPoint.changeUnit}<#else>mm/d</#if>。</w:t> </#if> <#else> <w:t>监测数据正在分析中...</w:t> </#if> </w:r> </w:p> </#if>
最新发布
09-11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值