Share sitemesh modify tagRule Technology in jsp, sitemeshtagrule

Source: Internet
Author: User
Tags return tag

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.