参考链接:
http://www.cnblogs.com/jason-star/p/3345225.html
http://blog.csdn.net/jsxzzliang/article/details/47022055
使用SHTML进行公共头部和底部的引用
SHTML介绍:shtml是一种基于SSI技术的文件,也就是Server Side Include--SSI 服务器端包含指令,一些Web Server如果有SSI功能的话就会对shtml文件特殊招待,服务器会先扫一次shtml文件看没有特殊的SSI指令存在,如果有的话就按Web Server设定规则解释SSI指令,解释完后跟一般html一起调去客户端。
SHTML的优点:使用ssi技术页面生成shtml文件,只用在头部文件位置加入<!--#include virtual="header.htm" -->/<!--#include file="header.htm" -->,其中第一个表示相对于服务器的路径,第二个表示文件的相对路径。然后修改的时候只要修改header.htm文件就可以了。使用shtml的好处是对搜索引擎比较友好,需要处理的文件在服务器端完成的,不会加重访问者的浏览器负担。
使用步骤:以tomcat作为服务器示例
1、找到Tomcatconfweb.xml 文件,打开注释 <servlet-name>ssi</servlet-name>
同时加入
<init-param>
<param-name>inputEncoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>outputEncoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<param-name>inputEncoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>outputEncoding</param-name>
<param-value>utf-8</param-value>
</init-param>
修改后效果如下:
<servlet>
<servlet-name>ssi</servlet-name>
<servlet-class>
org.apache.catalina.ssi.SSIServlet
</servlet-class>
<init-param>
<param-name>buffered</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>inputEncoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>outputEncoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>expires</param-name>
<param-value>666</param-value>
</init-param>
<init-param>
<param-name>isVirtualWebappRelative</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>4</load-on-startup>
</servlet>
<servlet-name>ssi</servlet-name>
<servlet-class>
org.apache.catalina.ssi.SSIServlet
</servlet-class>
<init-param>
<param-name>buffered</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>inputEncoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>outputEncoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>expires</param-name>
<param-value>666</param-value>
</init-param>
<init-param>
<param-name>isVirtualWebappRelative</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>4</load-on-startup>
</servlet>
2、打开mapping,同时修改.shtml为html。修改后效果如下:
<!-- The mapping for the SSI servlet -->
<servlet-mapping>
<servlet-name>ssi</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
3、修改Tomcatconfcontext.xml文件,修改效果如下:
<Context>改为:<Context privileged="true">,“表示享有特权的”
4、重启tomcat即可生效
5、页面嵌套html
页面1:
index.html
<! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> index</ title>
</ head>
< body>
<!--#include virtual = "common/header.html" -->
</ body>
</ html>
页面二:
header.html
<! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> index</ title>
</ head>
< body>
hello world!
</ body>
</ html>