通过引入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 ;

原文地址:https://www.cnblogs.com/qitian1/p/6462978.html