通过引入SiteMesh的JSP标签库,解决Freemarker与SiteMesh整合时,自定义SiteMesh标签的问题
不少web项目,都用到了SiteMesh。SiteMesh可以和JSP、Freemarker等模版进行整合,有一定的好处,当然也有其不好的地方。我个人觉得,如果没有必要,不要在项目中引入太多的工具和技术,工具多了,项目开发的速度提高不起来。
以前,主要是用JSP作为视图层,最近主要是用Freemarker,然后配置Freemarker和SiteMesh整合。
项目中,最初的做法如下所示。
<html>
<head>
${head}
</head>
<body>
<div id="pg-container">
<#include "common/header.ftl" />
${body}
<#include "common/footer.ftl" />
</div>
</body>
</html>
把页面的head部分,放到装饰模版的head部分,把页面的body部分,放到装饰模版的body里的某个位置。从而实现,简化引入通用JS、通用CSS、导航条、页脚等共用界面和样式。
后来,项目遇到一个问题,关于JS加载的问题。footer里引入了通用的JS,在页面最后加载。而页面的js,需要引入footer中的js,但实际上却在footer之前就被加载了,导致了JS引用错误的问题。
现在,想在footer之前,自己的页面body后面引入js,但是仍然要解决先后引入js的问题。
---------------------------------------------------
我探索到的方法:
普通展示页面,定义conten元素,不会在界面中直接展示出来,而是被放到装饰模版的footer之后,放置的位置取决于“<@decorator.getProperty property="page.footer"/>”的位置,很灵活。
<body>
<content tag="footer">
<script src="${static}/js/common.js" type="text/javascript"></script>
</content>
</body>
<body>
<div id="pg-container">
<#include "common/header.ftl" />
${body}
<#include "common/footer.ftl" />
</div>
<@decorator.getProperty property="page.footer"/>
</body>
其中遇到的一点技术问题,就是Freemarker与SiteMesh整合。
首先要说明,SiteMesh是“伪开源” 的,Maven中央仓库没有发现源码,郁闷。
SiteMesh与Freemarker整合,SiteMesh自带了个插件,com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet。
但是,在比较了代码的结构(通过class看结构,因为没有源码)和 JSP标签库提供的用法比较之后,发现,Freemarker插件功能不全,就提供了几个属性title,content等。
所以,我们通过在Freemarker引入SiteMesh的JSP标签库,实现自定义的。
Freemarker中引入标签库的语法:
<#assign decorator=JspTaglibs["http://www.opensymphony.com/sitemesh/decorator"] />
<#assign page=JspTaglibs["http://www.opensymphony.com/sitemesh/page"] />
注意哦,和JSP中的引入方式不一样,类似。
调用方式也不一样, <@decorator.getProperty property="page.footer"/>。
下面是JSP标签库的引入和调用方式:
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %> <decorator:getProperty property="page.content1"/>
补充2点:
1.我是参考了好几篇文章,才找到的解决办法。每篇文章都帮助我解决了一个或几个问题。
2.web.xml中配置taglib。
在Eclipse中会在taglib那一行有一个错误标志,但是运行的 时候没有问题……
原因:
所使用版本的问题,如果使用2.3版本就可以直接在<web-app>里面写
<taglib>
<taglib-uri>sitemesh-page</taglib-uri>
<taglib-location>/WEB-INF/lib/sitemesh-page.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>sitemesh-decorator</taglib-uri>
<taglib-location>/WEB-INF/lib/sitemesh-decorator.tld</taglib-location>
</taglib>
如果是用的是2.4版本,就不能直接这么写了,应该:
<jsp-config>
<taglib>
<taglib-uri>sitemesh-page</taglib-uri>
<taglib-location>/WEB-INF/lib/sitemesh-page.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>sitemesh-decorator</taglib-uri>
<taglib-location>/WEB-INF/lib/sitemesh-decorator.tld</taglib-location>
</taglib>
</jsp-config>
参考资料: http://blog.csdn.net/drift_away/article/details/8088758
http://www.cnblogs.com/mailingfeng/archive/2011/12/21/2296105.html
http://www.blogjava.net/usherlight/archive/2009/04/28/267879.html
http://www.iteye.com/problems/23502;
最新文章
- java web学习总结(二十九) -------------------JavaBean的两种开发模式
- Windows自动关机命令
- Eclipse远程调试(远程服务器端监听)
- AppCan中两种获取信息的方法
- Catch That Cow
- [JavaScript]plupload多图片上传图片
- Debian/Ubuntu Linux 下安装LLVM/Clang 编译器
- C++构造函数(二)
- quick-cocos2d-x教程1:在window上创建第一个项目文件夹,并制作helloworld
- Python核心编程笔记--私有化
- bootstrap——bootstrap-table(1)
- 「SDOI2017」树点涂色 解题报告
- Nhibernate 使用 (二)
- 精进之路之lru
- [Android][Framework]裁剪SystemServer服务以及关闭SystemFeature
- Java性能优化之编程技巧总结
- Python的命名空间及作用域
- Vue mixins(混入)
- vue-router + ElementUI实现NavMenu 导航菜单 选中状态的切换
- win7 默认程序设置
热门文章
- 【Codeforces Round #432 (Div. 2) B】Arpa and an exam about geometry
- 洛谷—— P1967 货车运输 || COGS——C 1439. [NOIP2013]货车运输
- BC 52 div2 A Victor and Machine
- Codeforces Round 363 Div. 1 (A,B,C,D,E,F)
- ex.Message ";ORA-01691: Lob 段 USER_MURPHY.SYS_LOB0000093717C00006$$ 无法通过 1024 (在表空间 ZJHH 中) 扩展";
- (素材源代码) 猫猫学IOS(五)UI之360等下载管理器九宫格UI
- SQL Server 2008 Tempdb 数据库迁移
- 代码适配Masonry使用的详细介绍
- Snail—UI学习之自己定义标签栏UITabBarController
- 键盘钩子监测按键后,获取键码及按键名称(MFC)