整合SiteMesh 之 20.3 整合Struts2与SiteMesh
来源:互联网 发布:mac系统ppt使用教程 编辑:程序博客网 时间:2024/05/04 08:37
在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这几个过滤器,示例代码如下:
- <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。示例代码如下:
- public class SiteMeshAction extends ActionSupport {
- public String execute() throws Exception {
- return SUCCESS;
- }
- }
接下来到struts.xml中配置这个Action,示例代码如下:
- <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成功。
- 整合SiteMesh 之 20.3 整合Struts2与SiteMesh
- struts2之整合sitemesh(sitemesh配置)
- struts2之整合sitemesh(使用sitemesh插件)
- struts2之整合sitemesh(简介)
- struts2 整合 SiteMesh
- Struts2整合SiteMesh
- sitemesh struts2 整合
- 整合SiteMesh 之 20.2 单独使用SiteMesh
- struts2+spring2+hibernate3整合SiteMesh
- struts2之整合sitemesh(定义装饰器页面)
- struts2之整合sitemesh(更复杂的装饰器页面)
- Struts2、Hibernate3、Spring2、Sitemesh、FreeMarker整合
- Struts2、Sitemesh、FreeMarker 最新版本整合配置
- ssh整合SiteMesh
- SSH2整合SiteMesh项目
- struts2之整合sitemesh(关于过滤一些不使用模板文件的注意事项)
- Struts2、FreeMarker、Sitemesh整合表现层的学习笔记
- struts2.2.1关于sitemesh和freemarker整合的细节
- 验证框架 之 10.4 内建验证器
- acm常见算法及例题
- Git详解之三 Git分支
- Struts2的Taglib 之 8.2 数据标签
- qt usb鼠标编译
- 整合SiteMesh 之 20.3 整合Struts2与SiteMesh
- 【数字认证】数字签名是什么?
- asp.net 论坛帖子的手工分页实现
- 整合SiteMesh 之 20.2 单独使用SiteMesh
- 第九天 CSS表单设计
- ARM 开发板 与 PC机ubuntu系统 nfs 挂载
- 整合SiteMesh 之 20.1 调整应用的风格
- Git详解之四 服务器上的Git
- 拦截器 之 6.4 两个有用的拦截器