hays

          海納百川
          posts - 25, comments - 48, trackbacks - 0, articles - 0
            BlogJava :: 首頁 ::  :: 聯系 :: 聚合  :: 管理
          解決 <%@ include file="/global/topright.html" %> 的中文亂碼問題 (引用)

          要解決這個問題,當然最簡單的就是在每個被 include 的檔案第一行,加上 <%@ page contentType="text/html;charset=big5" %> 這樣一定可以確保中文 jsp 檔不會出現亂碼,只不過,一旦程式修改成這樣的模式,你的程式就無法在舊的 jsp/servlet container 上執行了,因為舊的規格是不允被 include 檔案中再出現 <%@ page ... %> 這樣的定義的。

          況且,就算你願意為了 Tomcat 5.0.x 特別維護一套不同版本的 Source Code,你會遇到重大的挫折,因為 Tomcat 5.0.x 版在 charset 的設定上,會特別檢查 include 別人的程式與被人 include 的程式,這二個程式中所定義的 charset 是不是一樣,如果不一樣,在編譯時就會產生錯誤。更恐怖的是,竟然還分大小寫,比如說:"big5" "Big5" 這樣的定義,在 Tomcat 的認定上是不同的哦。

          假如有一個 Head.jsp,所有程式都會去 include 的檔案,試著在 Head.jsp 中,加上 <%@ page contentType="text/html;charset=big5" %> ,然後再執行,天啊,有人習慣用 "big5", 有人習慣用 "Big5",更有人習慣用 "BIG5",怎麼辦 ? 難道要一支一支程式去改嗎 ? 還是用 UltraEdit 的 Replace in Files,一次全部更正過來 ? 那日後,是不是得規定所有程式設計師統一採用 "Big5" 呢 ?

          想想,不太可行,也太愚蠢了。

          正確解決方案
          在 Tomcat 5.0.x 中,Tomcat 支援了 JSP 2.0 的規格,同時也支援了部分 J2EE 1.4 的規格,在 J2EE 1.4 的規格中,有關 JSP 的部份,有一個 <jsp-config> 的 XML Tag,這個 XML 區塊用來定義與 JSP 相關的特殊屬性,包含採用的 taglib 與 以下說明的 <jsp-property-group> ,而解決 include 檔中文問題的方法就定義在 <jsp-property-group> 中。

          首先,請至你開發的 webapps 的目錄下,找到 WEB-INF\web.xml 檔案,比如說:eip 專案的目錄,就是 $TOMCAT_HOME\webapps\eip\WEB-INF\web.xml。
          用文字編輯器開啟 web.xml,將以下這段 xml head 的定義 copy & replace 到 web.xml:
          														1
          2
          3
          4
          5
          														<?xml version="1.0" encoding="ISO-8859-1"?> 
          <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
          version="2.4">
          
          												

          特別注意,version="2.4",Tomcat 5.0.19 會去偵測這個版本資訊,目前只有 2.4 的才會處理 jsp-config 的參數。
          在 web.xml 中,加上以下這個區塊:
          														1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          														<jsp-config>
          <jsp-property-group>
             <description>
                Special property group for JSP Configuration JSP example.
             </description>
             <display-name>JSPConfiguration</display-name>
             <url-pattern>*.jsp</url-pattern>
             <el-ignored>true</el-ignored>
             <page-encoding>Big5</page-encoding>
             <scripting-invalid>false</scripting-invalid>
             <include-prelude></include-prelude>
             <include-coda></include-coda> 
          ?
             <description>
                Special property group for JSP Configuration JSP example.
             </description>
             <display-name>JSPConfiguration</display-name>
             <url-pattern>*.html</url-pattern>
             <el-ignored>true</el-ignored>
             <page-encoding>Big5</page-encoding>
             <scripting-invalid>false</scripting-invalid>
             <include-prelude></include-prelude>
             <include-coda></include-coda>
          </jsp-property-group>
          </jsp-config>
          
          												

          這二個區塊的定義,就是告知 Tomcat,在 eip 專案目錄下,所有的 .jsp, .html 檔案,若是沒有定義 contentType="text/html;charset=big5" 時,請採用預設的 "Big5" 字元集去處理,如此一來,你就不須要在每個 include 的檔案第一行加上 contentType="text/html;charset=big5" 了。
          想更進一步了解 <jsp-config> 的說明,請參考
          http://java.sun.com/xml/ns/j2ee/#oldresources

          web.xml 範例n
          														1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
          33
          34
          35
          36
          37
          38
          39
          40
          41
          														<?xml version="1.0" encoding="ISO-8859-1"?>
          ?
          <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
          version="2.4">
          <display-name>EIP 2E PROJECT</display-name>
          <description>EIP 2E PROJECT</description>
          ?
          <jsp-config>
          <jsp-property-group>
             <description>
                Special property group for JSP Configuration JSP example.
             </description>
             <display-name>JSPConfiguration</display-name>
             <url-pattern>*.jsp</url-pattern>
             <el-ignored>true</el-ignored>
             <page-encoding>Big5</page-encoding>
             <scripting-invalid>false</scripting-invalid>
             <include-prelude></include-prelude>
             <include-coda></include-coda>
          ?
             <description>
                Special property group for JSP Configuration JSP example.
             </description>
             <display-name>JSPConfiguration</display-name>
             <url-pattern>*.html</url-pattern>
             <el-ignored>true</el-ignored>
             <page-encoding>Big5</page-encoding>
             <scripting-invalid>false</scripting-invalid>
             <include-prelude></include-prelude>
             <include-coda></include-coda>
          </jsp-property-group>
          </jsp-config>
          ?
          <welcome-file-list>
          <welcome-file>index.jsp</welcome-file>
          <welcome-file>index.html</welcome-file>
          <welcome-file>index.htm</welcome-file>
          </welcome-file-list>
          </web-app>
          
          												

          評論

          # re: jsp中的include 亂碼問題解決  回復  更多評論   

          2006-10-14 13:34 by phinecos
          這個能一次性解決問題嗎?

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 平安县| 兴业县| 大同市| 北川| 大姚县| 榆树市| 平度市| 南宁市| 盐池县| 固阳县| 临汾市| 清河县| 阿坝| 阆中市| 五华县| 当涂县| 阿拉善左旗| 牙克石市| 兴仁县| 北碚区| 克东县| 乌兰县| 福建省| 绩溪县| 永年县| 汕尾市| 镇安县| 新建县| 边坝县| 喀喇沁旗| 泸定县| 宁武县| 华安县| 渑池县| 开江县| 上思县| 宁蒗| 巩留县| 夏河县| 马龙县| 龙川县|