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
废话少说,看看如果用 struts标签和freemarker标签应该如何来配置web.xml
< 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>
Properties | Content |
${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 |