配置tomcat讓shtml嵌套檔案顯示

來源:互聯網
上載者:User

標籤:cli   target   解析   nts   att   blog   技術分享   nbsp   底部   

之前,我知道tomcat可以直接解析shtml檔案,在瀏覽器中顯示效果來,後來由於需求發生改變,比如說 在做靜態化產生的時候一個網站的頭部和底部都是一樣的,如果每個頁面都產生一次,顯然很浪費時間,所有我們可以把 這個共同的頭部 和底部挖出來單獨產生,然後通過shtml嵌套顯示,就可以了。可是怎麼匯入靜態頭部和底部呢??有好的標籤嗎??有 shtml中有include標籤匯入外部檔案顯示。

例如:<!--#include virtual="${ctx}/web/header.html"-->(這個不是注釋,shtml匯入標籤就是這麼寫的)。

如果直接這樣子使用,tomcat是無法解析的。還需要在tomcat中做兩下簡單的配置:

第一:在 tomcat/conf/context.xml 中加上 privileged="true" ,表示享有特權的,

第二:在 tomcat/conf/web.xml中 關於name=“ssi”的servlet 配置取消掉注釋,包括該servlet的映射配置。

如下:

 

[html] view plain copy  print?
  1.      
  2.     <servlet>  
  3.         <servlet-name>ssi</servlet-name>  
  4.         <servlet-class>  
  5.           org.apache.catalina.ssi.SSIServlet  
  6.         </servlet-class>  
  7.         <init-param>  
  8.           <param-name>buffered</param-name>  
  9.           <param-value>1</param-value>  
  10.         </init-param>  
  11.         <init-param>  
  12.           <param-name>debug</param-name>  
  13.           <param-value>0</param-value>  
  14.         </init-param>  
  15.         <init-param>  
  16.           <param-name>expires</param-name>  
  17.           <param-value>666</param-value>  
  18.         </init-param>  
  19.         <init-param>  
  20.           <param-name>isVirtualWebappRelative</param-name>  
  21.           <param-value>0</param-value>  
  22.         </init-param>  
  23.        <!--- <span style="color:#FF0000;">這裡需要手動添加</span> -->  
  24.         <init-param>  
  25.           <param-name>inputEncoding</param-name>  
  26.           <param-value>UTF-8</param-value>  
  27.         </init-param>  
  28.         <init-param>  
  29.           <param-name>outputEncoding</param-name>  
  30.           <param-value>UTF-8</param-value>  
  31.         </init-param>  
  32.   
  33.         <load-on-startup>4</load-on-startup>  
  34.     </servlet>  
  35.   
  36.   
  37.   
  38.     <servlet-mapping>  
  39.         <servlet-name>ssi</servlet-name>  
  40.         <url-pattern>*.shtml</url-pattern>  
  41.     </servlet-mapping>  


備忘:紅色部分需要手動添加,不然在顯示的頁面中會亂碼。

 

基本上這樣子配置之後tomcat就回幫你解析shtml的include標籤了。感覺還是蠻方便,蠻強大的。

另外:

匯入的檔案可以不是完整的頁面,就是說可以不用包含html標籤,直接寫幾個div什麼的也是可以的。

如果在頁面上報錯:    [an error occurred while processing this directive]  是因為匯入的檔案不存在引發的。

配置tomcat讓shtml嵌套檔案顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.