jsp中sitemesh修改tagRule技術(shù)分享
sitemesh默認(rèn)提供了一些常用的rule
可以看到其實(shí)可以選擇
/** * Extracts the contents of any elements that look like * <code><content tag='foo'>...</content></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 BasicBlockRule<String> { 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)單的放入到任意位置?。?!
弊端
這樣雖然很簡(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ì)我們的支持。
上一篇:秒殺系統(tǒng)Web層設(shè)計(jì)的實(shí)現(xiàn)方法
欄 目:JSP編程
本文標(biāo)題:jsp中sitemesh修改tagRule技術(shù)分享
本文地址:http://mengdiqiu.com.cn/a1/JSPbiancheng/11438.html
您可能感興趣的文章
- 01-11在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
- 01-11jsp文件下載功能實(shí)現(xiàn)代碼
- 01-11JSP頁(yè)面跳轉(zhuǎn)方法大全
- 01-11jsp 使用request為頁(yè)面添加靜態(tài)數(shù)據(jù)的實(shí)例
- 01-11JSP servlet實(shí)現(xiàn)文件上傳下載和刪除
- 01-11SpringMail使用過(guò)程中的報(bào)錯(cuò)解決辦法
- 01-11JSP狀態(tài)管理的簡(jiǎn)單介紹
- 01-11jsp+servlet實(shí)現(xiàn)文件上傳與下載功能
- 01-11JSP的setProperty的使用方法
- 01-11Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件上傳(一)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-11web下載文件和跳轉(zhuǎn)的方法
- 01-11Spring注入Date類(lèi)型的三種方法總結(jié)
- 01-11在JSP中使用formatNumber控制要顯示的小
- 01-11Properties 持久的屬性集的實(shí)例詳解
- 01-11EJB3.0部署消息驅(qū)動(dòng)Bean拋javax.naming.Na
- 01-11jsp文件下載功能實(shí)現(xiàn)代碼
- 01-11JSP頁(yè)面跳轉(zhuǎn)方法大全
- 01-11詳解Spring的核心機(jī)制依賴(lài)注入
- 01-11jsp 使用request為頁(yè)面添加靜態(tài)數(shù)據(jù)的實(shí)
- 01-11Spring獲取ApplicationContext對(duì)象工具類(lèi)的
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?