在Struts2中使用SiteMesh的时候,SiteMesh是以Struts2插件的形式出现的。
单独使用SiteMesh需要做好多事情:准备资源、引用过滤器、准备模板页面、准备被装饰页面、结合模板页面和被装饰页面。
在整合Struts2与SiteMesh的时候,准备模板页面和准备被装饰页面基本上没有变化,这里就不过多叙述了。接下来从其他几个方面,包括:准备资源、引用过滤器、结合模板页面和被装饰页面,来继续学习。
20.3.1准备资源
这里的准备资源和单独使用SiteMesh稍有不同,接下来把需要的东西一一拷贝到我们学习Struts2的Web项目上去:
- SiteMesh的jar包:把sitemesh-blank\WEB-INF\lib\sitemesh-2.4.1.jar拷贝到Web工程的构建路径下,WebContent\WEB-INF\lib包下面。
- SiteMesh插件:把struts-2.1.8.1\lib\struts2-sitemesh-plugin-2.1.8.1.jar拷贝到Web工程的构建路径下,WebContent\WEB-INF\lib包下面。
- SiteMesh的自定义标签:把sitemesh-blank\WEB-INF\lib包下的所有tld文件拷贝到Web工程的WEB-INF文件夹下。注意,前面单独使用SiteMesh的时候,是把tld文件拷贝到WEB-INF\lib文件夹下的。
- 模板页面和被装饰页面的结合:把sitemesh-blank\WEB-INF\decorators.xml拷贝到Web工程的WEB-INF文件夹下。
20.3.2引用过滤器
ings��-i�� �,� amily:Wingdings'>Ø sitemesh-blank\WEB-INF\ decorators.xml:定义模板页面和被装饰页面如何结合。
20.2.2拷贝资源
引用SiteMesh的过滤器本身和前面示例做的差不多,就是把相关的过滤器的配置,拷贝到web.xml中,但要注意过滤器的关系:
- FilterDispatcher一定要有,通常出现在最后。
- 如果出现了SiteMesh这种过滤器,需要在SiteMesh之前加上ActionContextCleanUp过滤器。
所以,不但要引用SiteMesh过滤器,还要在它之前引用Struts2的ActionContextCleanUp过滤器,以<filter-mapping>元素出现的顺序为准,应该顺序配置ActionContextCleanUp、SiteMesh、FilterDispatcher这几个过滤器,示例代码如下:
java代码:
- <filter>
- <filter-name>Struts2-action-cleanup</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>Struts2-action-cleanup</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter>
- <filter-name>sitemesh</filter-name>
- <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>sitemesh</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter>
- <filter-name>Struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>Struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
20.3.3结合模板页面和被装饰页面
模板页面和被装饰页面基本上没有变化,只是模板页面引用SiteMesh的tld文件的路径,原来是“WEB-INF\lib\sitemesh-decorator.tld”,现在应该去掉lib,修改成“WEB-INF\sitemesh-decorator.tld”。
现在要使用Struts2的Action,先来准备一个简单的Action,没有什么功能,只是跳转到后继的jsp。示例代码如下:
java代码:
- public class SiteMeshAction extends ActionSupport {
- public String execute() throws Exception {
- return SUCCESS;
- }
- }
接下来到struts.xml中配置这个Action,示例代码如下:
java代码:
- <package name="helloworld" extends="struts-default">
- <action name="sitemeshAction" class="cn.javass.sitemesh.SiteMeshAction">
- <result>/index.jsp</result>
- lt;/action>
- </package>
decorators.xml文件也不用变化,因为原来配置的装饰模式就是“/*”,意思是当前web应用中所有的URL指向的页面都会受到装配。
运行测试一下,URL为: http://localhost:9080/helloworld/sitemeshAction.action,会得到与单独使用SiteMesh一样的结果,这样说明Struts2整合SiteMesh成功。
私塾在线网站原创《研磨struts2》系列
转自请注明出处:【http://sishuok.com/forum/blogPost/list/0/4180.html】
欢迎访问http://sishuok.com获取更多内容
分享到:
相关推荐
Struts 2.0提供一个Sitemesh插件,允许在Sitemesh模板中使用Struts标记。 要使用Sitemesh需要包含Freemark,Sitemesh和Sitemesh插件库文件。 配置过滤器 如果需要使用Freemark模板文件作为装饰器文件,需要在web....
NULL 博文链接:https://takeme.iteye.com/blog/1716488
一个Struts2+Spring2.5+SiteMesh的小DEMO,仅供大家做个参考!我也刚学会。欢迎大家多交流!
第10章 Struts 2的标签库,第11章 Struts 2的Ajax支持,第12章 使用FreeMarker充当表现层,第13章 整合Spring,第14章 整合JSF,第15章 整合SiteMesh,第16章 整合JasperReports,第17章 整合JFreeChart,第18章 ...
struts2与sitemesh整合所需的两个最新架包sitemesh-2.4.2,struts2-sitemesh-plugin-2.1.8
struts2-sitemesh-plugin-2.2.1.1.jarstruts2
struts2-sitemesh-plugin-2.0.8.jar
struts2-sitemesh-plugin-2.1.6.jar
struts2-2拦截器与SiteMeshstruts
struts2+sitemesh例子 该例子描述了struts2+sitemesh的配置,比较实用
Struts2+Spring+Hibernate+sitemesh做的一个在线音乐站点写的一个在线音乐 的站点.功能实现的相对比较粗糙(如歌曲列表没考虑分页什么的),另外只能使用IE(使用了ActiveX 控件播放音乐的).
struts2-sitemesh_入门收集struts2-sitemesh_入门收集
struts框架与sitemesh框架整合的插件,在java web开发中可以制作模板页,使自己做的网站看起来更专业
spring-boot-sitemesh 源码整合
struts2-sitemesh-plugin-2.1.8.1.jar
struts2sitemesh-freemarker 源码
包含struts2-core-2.5.10.1.jar,struts2-jfreechart-plugin-2.5.10.1.jar,struts2-json-plugin-2.5.10.1.jar,struts2-junit-plugin-2.5.10.1.jar,struts2-bean-validation-plugin-2.5.10.1.jar,struts2-cdi-...
freemarker 与sitemesh 整合!是一个不错的例子
解决 java.lang.NoSuchFieldException: DEFAULT_PARAM>问题,下载后替换即可。
Sitemesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。它能帮助我们在由大 量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的banner,一致的版权,等等。 它不仅仅能处理动态的...