看公司项目代码的时候,主页下找不到对应的侧边栏和头部以及脚的代码,也没有发现include标签存在,仔细观察下发现是引入了sitemesh标签,才发现原来是用了sitemesh网页布局框架!!!
之前的项目模块重用一般都是include **.jsp文件,并没有感觉到多麻烦,但是在使用了了解了sitemesh后,发现页面重用其实可以更简单。而且sitemesh也适用于.asp和html。
使用步骤如下:(sitemesh运行环境需要:servlet, JDK,SiteMesh运行时库)
1,引入maven依赖
<dependency><groupId>opensymphony</groupId>
<artifactId>sitemesh</artifactId>
<version>2.4.2</version>
</dependency>
<!--SiteMesh-->
<filter-name>sitemeshFilter</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemeshFilter</filter-name>
<url-pattern>/*</url-pattern> <!--/* 是要被装饰的页面,如是“ /* ”指对WebContent/目录下的所有JSP页面进行装饰-->
</filter-mapping>
3.在WEB-INF目录下添加sitemesh配置文件decorators.xml
<decorators defaultdir="/WEB-INF/views">
<!-- 默认装饰页面, 在需要装饰的页面增加<meta name="decorator" content="blank"/> -->
</decorators>
在common.jsp中写好可以复用的代码,其中<sitemesh:title/>:表示的是在这里插入的是被装饰页面的title标签里面的内容。<sitemesh:head/>:表示在这里插入的是被装饰页面的头部标签里面的内容。<sitemesh:body/>:表示这里插入的是被装饰页面的body标签里面的内容。
在common.jsp中可以在<sitemesh:head/>和<sitemesh:body/>这两个标签上下穿插内容,使其显示在页面相应的地方。
5.在需要被装饰的页面中,入index.jsp,需要加上以下标签,表明该页面是被common.jsp装饰的。<meta name="decrator" content="common"/>
其中index.jsp中的<head></head>内容,则被加载到common.jsp中的<sitemesh:head/>这里;<body></body>中的内容,则被加载到common.jsp的<sitemesh:body/>这里;<title></title>同理。
以上是本人对sitemesh的初步认识,如有错误,希望各路大神出手指出。