佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > jsp中sitemesh修改tagRule技術(shù)分享

jsp中sitemesh修改tagRule技術(shù)分享

熱門(mén)標(biāo)簽:Linux服務(wù)器 科大訊飛語(yǔ)音識(shí)別系統(tǒng) Mysql連接數(shù)設(shè)置 團(tuán)購(gòu)網(wǎng)站 服務(wù)器配置 電子圍欄 銀行業(yè)務(wù) 阿里云

sitemesh默認(rèn)提供了一些常用的rule

可以看到其實(shí)可以選擇

/**
 * Extracts the contents of any elements that look like
 * code>lt;content tag='foo'gt;...lt;/contentgt;/code> and write the contents
 * to a page property (page.foo).
 *
 * p>This is a cheap and cheerful mechanism for embedding multiple components in a
 * page that can be used in different places in decorators./p>
 *
 * @author Joe Walnes
 */
public class ContentBlockExtractingRule extends BasicBlockRuleString> {
 private final ContentProperty propertyToExport;
 public ContentBlockExtractingRule(ContentProperty propertyToExport) {
  this.propertyToExport = propertyToExport;
 }
 @Override
 protected String processStart(Tag tag) throws IOException {
  tagProcessorContext.pushBuffer();
  return tag.getAttributeValue("tag", false);
 }
 @Override
 protected void processEnd(Tag tag, String tagId) throws IOException {
  propertyToExport.getChild(tagId).setValue(tagProcessorContext.currentBufferContents());
  tagProcessorContext.popBuffer();
 }
}

修改ScriptTagRuleBundle處理如下

public class ScriptTagRuleBundle implements TagRuleBundle {
 @Override
 public void install(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
  defaultState.addRule("content", new ContentBlockExtractingRule(contentProperty.getChild("page")));
 }
 @Override
 public void cleanUp(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
 }
}

用法很簡(jiǎn)單使用content作為tag默認(rèn)填上tag即可

比如

content tag="reference">
script type="text/javascript" src="%=path%>/plugins/select2/js/select2.min.js">/script>
script type="text/javascript" src="%=path%>/plugins/select2/js/i18n/zh-CN.js">/script>
script type="text/javascript" src="%=path%>/plugins/bootstrap-modal/js/bootstrap-modal.js">/script>
script type="text/javascript" src="%=path%>/plugins/bootstrap-modal/js/bootstrap-modalmanager.js">/script>
/content>

在模板中這樣

body class="mainBody">
sitemesh:write property='body'/>
sitemesh:write property='page.reference'/>
/body>

這樣就可以很簡(jiǎn)單的放入到任意位置!?。?/p>

弊端

這樣雖然很簡(jiǎn)單 但是也存在一些問(wèn)題 開(kāi)發(fā)如果需要增加新的content必須要要到母版頁(yè)【對(duì)的 其實(shí)sitemesh不就像是asp.net中的母版頁(yè)么】

增加對(duì)應(yīng)的sitemesh:write標(biāo)簽

propertyToExport.getChild(tagId).setValue(tagProcessorContext.currentBufferContents());

并且上述代碼中同樣存在覆蓋的問(wèn)題 比如多處使用了同樣的tagId

解決

sitemesh似乎沒(méi)有提供直接用來(lái)拼接多個(gè)的tagRule

如果有需求將某塊元素放入到末尾 可以考慮增加tagRule

在processEnd時(shí)直接將對(duì)應(yīng)的元素直接append

最終可以直接輸出

以上就是我們給大家整理的本次教程的全部?jī)?nèi)容,感謝你對(duì)腳本之家的支持。

您可能感興趣的文章:
  • Java SiteMesh新手學(xué)習(xí)教程代碼案例
  • 如何解決SpringBoot2.x版本對(duì)Velocity模板不支持的方案
  • SpringBoot與velocity的結(jié)合的示例代碼
  • JAVA velocity模板引擎使用實(shí)例
  • html文件中jquery與velocity變量中的$沖突的解決方法
  • velocity顯示List與Map的方法詳細(xì)解析
  • Velocity基本語(yǔ)法介紹
  • SiteMesh如何結(jié)合Freemarker及velocity使用

標(biāo)簽:棗莊 廣元 衡水 衢州 大理 江蘇 萍鄉(xiāng) 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《jsp中sitemesh修改tagRule技術(shù)分享》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266
    临洮县| 萨嘎县| 平舆县| 敦煌市| 青阳县| 江城| 常熟市| 普兰店市| 大名县| 商水县| 鹤壁市| 马龙县| 吴桥县| 邹城市| 和龙市| 崇左市| 敖汉旗| 南充市| 吕梁市| 北宁市| 屏东市| 呈贡县| 铜川市| 海口市| 海安县| 长岭县| 双流县| 建宁县| 铜梁县| 南木林县| 灵璧县| 梁河县| 碌曲县| 东乌珠穆沁旗| 锡林郭勒盟| 弋阳县| 富阳市| 沿河| 江阴市| 布尔津县| 甘泉县|