谈谈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 >

在模板中,原来的<decorator:head/><decorator:body/>之类要改变一下了,下面举个例子

With the following decorated page :-

 <html>
<meta name="author" content="tm_jee" />
<head>
<title>My Title</title>
<link rel="stylesheet" type="text/css" href="mycss.css" />
<style type="text/javascript" language="javascript" src="myjavascript.js"></script>
</head>
<body<
<h1>Sample</h1>
</body>
</html>

PropertiesContent
${title}My Title
${head}<link rel="stylesheet" type="text/css" href="mycss.css" /> <style type="text/javascript" language="javascript" src="myjavascript.js"></script>
${body}<h1>Sample</h1>
${page.properties.meta.author}tm_jee



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值