Share sitemesh modify tagRule Technology in jsp, sitemeshtagrule
Sitemesh provides some common rule by default.
You can see that you can choose
/** * 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(); }}
The process for modifying ScriptTagRuleBundle is as follows:
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) { }}
The usage is simple. Use content as the tag by default and fill in the tag.
For example
<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>
In the template
<body class="mainBody"><sitemesh:write property='body'/><sitemesh:write property='page.reference'/></body>
In this way, you can easily put it in any location !!!
Disadvantages
This is simple, but there are still some problems. If you need to add new content, you must go to the master page. [In fact, sitemesh is not like a master page in asp.net]
Add the corresponding sitemesh: write tag
propertyToExport.getChild(tagId).setValue(tagProcessorContext.currentBufferContents());
In addition, the above Code also has the problem of overwriting. For example, the same tagId is used in multiple places.
Solution
Sitemesh does not seem to provide directly used to splice multiple tagRule
If you want to add a certain element to the end, you can consider adding tagRule.
Directly append the corresponding elements during processEnd.
The final output can be directly
The above is all the content of this tutorial. Thank you for your support for the help house.