jsp中sitemesh修正tagRule技術共有


sitemeshはデフォルトでよく使われているruleを提供します。

見えますが、選択できます。

/**
 * Extracts the contents of any elements that look like
 * <code>&lt;content tag='foo'&gt;...&lt;/content&gt;</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();
 }
}
Script TagRuleBunleを修正しました。次のように処理します。

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) {
 }
}
使い方が簡単でcontentを使っています。
たとえば

<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>
これで簡単にどこにでも入れることができます!!!
不正行為
このように簡単ですが、いくつかの問題があります。もし新しいcontentを増やす必要があれば、マザーページに行かなければなりません。
対応するsitemesh:writeタグを追加します。

propertyToExport.getChild(tagId).setValue(tagProcessorContext.currentBufferContents());
また、上記のコードにもカバーの問題があります。例えば、同じtagIdを多く使っています。
解決
sitemeshは複数のタイルを直接つなぎ合わせるためのtagRuleを提供していないようです。
需要があれば、ある元素を末尾に入れると、tagRuleを増やすことが考えられます。
processEndで直接に対応する要素をapped
最終的に直接出力できます。
以上は私達がまとめた今回の教程の内容です。応援ありがとうございます。