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ì)腳本之家的支持。
相關(guān)文章
(jsp/html)網(wǎng)頁(yè)上嵌入播放器(常用播放器代碼整理)
網(wǎng)頁(yè)上嵌入播放器,只要在HTML上添加以上代碼就OK了,下面整理了一些常用的播放器代碼,總有一款適合你,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-05-05二、設(shè)置開(kāi)發(fā)、運(yùn)行環(huán)境
二、設(shè)置開(kāi)發(fā)、運(yùn)行環(huán)境...2006-10-10關(guān)于JSP的一點(diǎn)疑問(wèn)小結(jié)
我在做一個(gè)JSP小測(cè)試.寫(xiě)的一些代碼如下2009-02-02jsp無(wú)法提交nicEdit中的內(nèi)容的解決方法
之前在測(cè)試頁(yè)面功能時(shí),發(fā)現(xiàn)在input type=“text”的輸入欄中按下回車(chē)鍵時(shí),會(huì)導(dǎo)致form表單的自動(dòng)提交,遂將提交按鈕由submit類(lèi)型改為button類(lèi)型。這樣雖然解決了表單自動(dòng)提交的問(wèn)題,但是卻導(dǎo)致了新問(wèn)題的出現(xiàn),即表單中的nicEdit的值無(wú)法被提交了。下邊是解決的方法,需要的朋友請(qǐng)往下看2013-03-03Struts2.5 利用Ajax將json數(shù)據(jù)傳值到JSP的實(shí)例
下面小編就為大家?guī)?lái)一篇Struts2.5 利用Ajax將json數(shù)據(jù)傳值到JSP的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09Tomcat網(wǎng)站發(fā)布配置方案詳細(xì)說(shuō)明
Tomcat網(wǎng)站發(fā)布配置方案詳細(xì)說(shuō)明,需要的朋友可以參考一下2013-03-03