谈谈struts2对SiteMesh的扩展,以及龌龊的ActionContextCleanUp命名

        早听所struts2.0对SiteMesh进行了扩展,增加了FreeMarker模板的支持,今晚有空特意去找了一些相关的资料来,希望以后在项目中可以派上用途。总结起来,它做了3个方面的扩展
1、可以在SiteMesh模板中使用struts标签,这个是招牌,当然要对其扩展。
2、增加了FreeMarker模板,这个是我们所迫切需要的。将com.opensymphony.module.sitemesh.filter.PageFilter 扩展为了 org.apache.struts2.sitemesh.FreeMarkerPageFilter,以后直接使用后者就可以了
3、同上,增加了Velocity模板   (老外就喜欢这样,总是把一个问题复杂化,弄出很多技术,再来集成一下)

在模板中使用struts标签必须增加一个叫 ActionContextCleanUp 的过滤器,这个取名我觉得真是误人子弟,我一开始就琢磨不透了,为了在模板中使用Struts标签,理应不要CleanUp才是呀,真是昏死,我只好去查看相关文档


Special filter designed to work with the FilterDispatcher and allow for easier integration with SiteMesh. Normally, ordering your filters to have SiteMesh go first, and then FilterDispatcher go second is perfectly fine. However, sometimes you may wish to access Struts features, including the value stack, from within your SiteMesh decorators. Because FilterDispatcher cleans up the ActionContext, your decorator won't have access to the data you want. ( How to fix this problem?)

By adding this filter, the FilterDispatcher will know to not clean up and instead defer cleanup to this filter. The ordering of the filters should then be:

  • this filter
  • SiteMesh filter
  • FilterDispatcher
看到了吧,原来是defer cleanup,原来如此,那么命名理应叫 ActionContexDeferCleanUp 才有助于用户理解呀,哎,害的我们这些英语死差的人琢磨半天,还要硬着头皮去看文档,不过老外可能是说出了cleanup这个意思而已吧,难道文化差异~?HOHO

废话少说,看看如果用  struts标签和freemarker标签应该如何来配置web.xml


     < filter >
        
< filter-name > struts-cleanup filter-name >
        
< filter-class > org.apache.struts2.dispatcher.ActionContextCleanUp filter-class >
    
filter >
    
< filter >
        
< filter-name > sitemesh filter-name >
        
< filter-class > org.apache.struts2.sitemesh.FreeMarkerPageFilter filter-class >
    
filter >
    
< filter >
        
< filter-name > struts filter-name >
        
< filter-class > org.apache.struts2.dispatcher.FilterDispatcher filter-class >
    
filter >

    
< filter-mapping >
        
< filter-name > struts-cleanup filter-name >
        
< url-pattern > /* url-pattern >
    
filter-mapping >
    
< filter-mapping >
        
< filter-name > sitemesh filter-name >
        
< url-pattern > /* url-pattern >
    
filter-mapping >
    
< filter-mapping >
        
< filter-name > struts filter-name >
        
< url-pattern > /* url-pattern >
    
filter-mapping >

在模板中,原来的之类要改变一下了,下面举个例子

With the following decorated page :-


Properties Content
${title} My Title
${head}