在SSH框架中增加SiteMesh的支持
1)引入jar包,如下两个jar包需要导入到系统的lib文件夹中:
sitemesh-2.4.jarstruts2-sitemesh-plugin-2.2.1.1.jar
2)修改web.xml增加相应的过滤器<!-- use ContextLoaderListener initialize Spring container --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- difine ActionContextCleanUp filter --><filter><filter-name>struts-cleanup</filter-name><filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class></filter><!-- define SiteMesh core filter--><filter><filter-name>sitemesh</filter-name><filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class></filter><!-- Struts 2 core Filter --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><!-- first: cleanup filter --><filter-mapping><filter-name>struts-cleanup</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- second: sitmesh core filter --><filter-mapping><filter-name>sitemesh</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- third: struts2 core filter --><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list<welcome-file>index.html</welcome-file></welcome-file-list>
3)在web.xml同级增加一个sitmesh的配置文件decorators.xml,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?><decorators defaultdir="/decorators"> <excludes> </excludes> <decorator name="main" page="main.jsp"> <pattern>/*</pattern> </decorator> <decorator name="menu" page="menu.jsp"/></decorators>
4)在decorators文件夹中增加模板文件main.jsp 以及menu.jsp:main.jsp文件如下:
<%@ page contentType="text/html; charset=UTF-8" language="java" errorPage="" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%><%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%><%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title><decorator:title default="AdminPortal title"/></title><s:head/></head><body><table width="95%" border="1" align="center"><tr><td colspan="2"><page:applyDecorator page="/jsp/top.html" name="menu"/></td></tr><tr><td width="15%"><page:applyDecorator page="/jsp/menu.html" name="menu"/></td><td><decorator:body/></td></tr></table></body></html>
menu.jsp文件如下:
<%@ page contentType="text/html; charset=UTF-8" language="java" errorPage="" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%><%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%><%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title><decorator:title default="AdminPortal title"/></title><s:head/></head><body><table width="50%"><tr><td><decorator:body/></td></tr></table></body></html>
再次运行的时候,就可以看到如下的内容:
作者 陈字文(热衷于PM\ORACLE\JAVA等,欢迎同行交流)EMAIL:ziwen@163.com QQ:409020100