jsp中sitemesh修改tagRule技术分享

所属分类: 网络编程 / JSP编程 阅读数: 1066
收藏 0 赞 0 分享

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();
 }
}

修改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) {
 }
}

用法很简单使用content作为tag默认填上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>

这样就可以很简单的放入到任意位置!!!

弊端

这样虽然很简单 但是也存在一些问题 开发如果需要增加新的content必须要要到母版页【对的 其实sitemesh不就像是asp.net中的母版页么】

增加对应的sitemesh:write标签

propertyToExport.getChild(tagId).setValue(tagProcessorContext.currentBufferContents());

并且上述代码中同样存在覆盖的问题 比如多处使用了同样的tagId

解决

sitemesh似乎没有提供直接用来拼接多个的tagRule

如果有需求将某块元素放入到末尾 可以考虑增加tagRule

在processEnd时直接将对应的元素直接append

最终可以直接输出

以上就是我们给大家整理的本次教程的全部内容,感谢你对脚本之家的支持。

更多精彩内容其他人还在看

table中点击表头实现排序的功能示例介绍

获取上次点击的表头的名称和这次点击的表头的名称做比较,如果两者相同就按原先相反的顺序排列,否则新列升序排列
收藏 0 赞 0 分享

jsp防止跨域提交数据的具体实现

这篇文章主要介绍了jsp防止跨域提交数据的具体实现,需要的朋友可以参考下
收藏 0 赞 0 分享

解决jsp页面使用网络路径访问图片的乱码问题

这篇文章主要介绍了jsp页面使用网络路径访问图片的乱码问题的解决方法 ,需要的朋友可以参考下
收藏 0 赞 0 分享

在jsp页面如何获得url参数

这篇文章主要介绍了在jsp页面获得url参数的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

几种防止表单重复提交的方法

表单重复提交是在多用户Web应用中最常见的一个问题,很多场合都会遇到这种情况,下面与大家分享下防止表单重复提交的几种策略
收藏 0 赞 0 分享

struts2集成javamail发邮件示例详解

这篇文章主要介绍了struts2集成javamail发邮件示例,需要的朋友可以参考下
收藏 0 赞 0 分享

不让tomcat显示目录文件列表的配置方法

这篇文章主要介绍了不让tomcat显示目录文件列表的配置方法,这里需要修改conf/web.xml文件,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp使用cookie存储中文示例分享

这篇文章主要介绍了jsp使用cookie存储中文示例,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp中include指令静态导入和动态导入的区别详解

这篇文章主要介绍了jsp中include指令静态导入和动态导入的区别,通过示例和图文讲解可以更好的看出他们的区别,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp中文显示问号问题解决方法

jsp中想要输出的中文被显示成问号?在eclipse-windows- preferences中搜索jsp,Encoding选项中选择 Chinese,此问题便可解决
收藏 0 赞 0 分享
查看更多