麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

struts中文問(wèn)題和國(guó)際化問(wèn)題的終極解決方案

2019-11-18 15:56:00
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  java本身就支持多國(guó)語(yǔ)言編碼,不需要寫(xiě)任何程序,可以很簡(jiǎn)單的 實(shí)現(xiàn)。 秘訣就是兩點(diǎn):
  
  1、所有Html/jsp頁(yè)面全部采用UTF-8編碼
  
  2、客戶端瀏覽器完全支持UTF-8編碼
  
  步驟:
  1、首先把所有的HTML/JSP的ContentType都設(shè)為UTF-8
  
  2、然后對(duì)于JSP程序中的非ASCII碼提示信息都不應(yīng)該寫(xiě)在程序里面,都應(yīng)該放在
  application.PRoperties里面統(tǒng)一治理。
  
  3、對(duì)HTML用native2ascii工具統(tǒng)一做一次處理,把HTML中的非ASCII碼都轉(zhuǎn)換為Unicode編碼。
  
  4、針對(duì)不同的語(yǔ)言,寫(xiě)不同的application.properties,比如說(shuō)簡(jiǎn)體中文是
  application_zh_CN.properties,繁體中文是application_zh_TW.properties這樣,然后對(duì)這些配置信
  息文件同樣用native2ascii工具處理一次,把非ASCII碼統(tǒng)統(tǒng)轉(zhuǎn)為Unicode編碼。
  
  5、在Servlet的request.getCharacterEncoding()獲得客戶端的操作系統(tǒng)默認(rèn)編碼,然后set到Struts
  的HTTPsession的Locale中。
  
  OK!現(xiàn)在不同的客戶訪問(wèn),就會(huì)顯示不同的語(yǔ)言版本了。你可以看看此時(shí)你的瀏覽器的字符集,就是
  UTF-8。現(xiàn)在你的網(wǎng)站和Google一樣了,嘿嘿,其實(shí)你有心的話,看看你的瀏覽器訪問(wèn)Google的時(shí)候是
  什么字符集吧
  
  切記:所有的HTML/JSP都要設(shè)為UTF-8編碼,所有的文件中的非ASCII碼字符都要用native2ascii工具轉(zhuǎn)
  為用ASCII表示的Unicode編碼。
  
  --------------------------------------
  上面所述是我從網(wǎng)上下的一篇于中文問(wèn)題的解決方案,確切的說(shuō)應(yīng)該是關(guān)于Struts的國(guó)際化問(wèn)題,下面我結(jié)合我的實(shí)踐談?wù)劸唧w如何實(shí)現(xiàn)Struts的國(guó)際化問(wèn)題,我對(duì)理論不是非常精通,我只能完全憑自己的理解和實(shí)踐來(lái)講述,所以下面講的內(nèi)容可能不是非常正確,還請(qǐng)大家原諒。但有一點(diǎn)可以肯定,我通過(guò)自己的努力解決了Struts的中文問(wèn)題,并實(shí)現(xiàn)Struts的國(guó)際化,其實(shí)一切并不復(fù)雜,下面是具體步驟:
  
  0.碰到的問(wèn)題(這些問(wèn)題也許不會(huì)同時(shí)出現(xiàn))
  a.中文數(shù)據(jù)從數(shù)據(jù)庫(kù)中到j(luò)sp中后就變成了"????"
  b.做好的中文properties文件,其中的中文value在頁(yè)面顯示亂碼
  c.jsp文件中的中文到瀏覽器后顯示時(shí)也是亂碼(建議不要在jsp文件中輸入中文,盡量放在properties文件中)
  d.由jsp傳給bean的中文值,再由bean傳回頁(yè)面又是亂碼
  e.當(dāng)更換本地瀏覽器的語(yǔ)言選項(xiàng)時(shí),Web應(yīng)用程序不能自動(dòng)根據(jù)你的locale選擇合適的*.properties文件。導(dǎo)致Web應(yīng)用程序不能?chē)?guó)際化。
  1.環(huán)境:
  Web服務(wù)器: Tomcat 5.0.19
  操作系統(tǒng): Win2000 Server
  JVM : jdk 1.4
  數(shù) 據(jù) 庫(kù): Oracle 8.1.7
  開(kāi)發(fā)工具: struts studio 5.2 pro for eclipse
  2.先將所有*.jsp 網(wǎng)頁(yè)中開(kāi)頭處加入
  
  再設(shè)置
  3.然后編輯好兩個(gè)*.properties文件,放在classes文件夾下你指定的地方,這里是放在/web-inf/classes/com/wiley 下,它們分別是:
  
  ApplicationResources.properties (英文資源文件)
  ApplicationResources_zh.properties (中文資源文件)
  隨便用什么工具編寫(xiě)都行啊!
  4.將ApplicationResources_zh.properties轉(zhuǎn)碼成gb2312。上面引文說(shuō)要轉(zhuǎn)成UTF-8,結(jié)果我試了,不行。轉(zhuǎn)成gb2312就行了,操作是。
  將ApplicationResources_zh.properties更名為ApplicationResources_xx.properties
  在DOS命令行進(jìn)入ApplicationResources_xx.properties所在的文件夾
  使用命令:native2ascii -encoding gb2312 ApplicationResources_xx.properties ApplicationResources_zh.properties(至于你為什么會(huì)出現(xiàn)“native2ascii不是內(nèi)部命令”,,請(qǐng)查其它資料,可能你要設(shè)置環(huán)境變量,因?yàn)樗莏dk的文件夾bin下的一個(gè)應(yīng)用程序)
  5.接下來(lái)配置struts-config.xml,很簡(jiǎn)單,我們加入: 就行了;
  
  到此已能解決大多數(shù)中文問(wèn)題。如上面所說(shuō)的a,b,e 現(xiàn)在打開(kāi)瀏覽器,選擇菜單:工具》internet選項(xiàng)》語(yǔ)言,將“中文-中國(guó)[zh-cn]”刪掉,添加一個(gè)“英語(yǔ)-英國(guó)[zh-gb]”確定后,重啟Tomcat,輸入網(wǎng)址你就會(huì)發(fā)現(xiàn),你的頁(yè)面的文本信息就會(huì)用的是ApplicationResources.properties (英文資源文件)中的內(nèi)容。假如換回“中文-中國(guó)[zh-cn]”,它就會(huì)顯示ApplicationResources_zh.properties (中文資源文件)中的中文內(nèi)容。
  
  至于問(wèn)題“c.jsp文件中的中文到瀏覽器后顯示時(shí)也是亂碼” 你就要用與第4步類似的方法來(lái)重新對(duì)*.jsp 文件編碼,這時(shí)-encoding的參數(shù)就要用UTF-8了,假如你用的也是struts studio 5.2 pro for eclipse工具,這一步就免了。它會(huì)自動(dòng)用UTF-8的格式存儲(chǔ)。
  至于問(wèn)題“d.由jsp傳給bean的中文值,再由bean傳回頁(yè)面又是亂碼”的解決,我只是加了個(gè)過(guò)濾器。
  你可以現(xiàn)在web.xml中加入:
  
  Set Character Encoding
  com.wiley.SetCharacterEncodingFilter
  
  encoding
  utf-8
  
  ignore
  true
  
  Set Character Encoding
  action
  
  然后在你指定的包內(nèi)加個(gè)java文件 我放在了/web-inf/classes/com/wiley 里,下面是源代碼:
  /*
  * XP Forum
  *
  * Copyright (c) 2002-2003 RedSoft Group. All rights reserved.
  *
  */
  package com.huahang.tj.struts.filters;
  
  import javax.servlet.*;
  import java.io.IOException;
  
  /**
  *
  Filter that sets the character encoding to be used in parsing the
  * incoming request, either unconditionally or only if the client did not
  * specify a character encoding. Configuration of this filter is based on
  * the following initialization parameters:
  
  *
  
  *
  encoding - The character encoding to be configured
  * for this request, either conditionally or unconditionally based on
  * the ignore initialization parameter. This parameter
  * is required, so there is no default.
  *
  ignore - If set to "true", any character encoding
  * specified by the client is ignored, and the value returned by the
  * selectEncoding() method is set. If set to "false,
  * selectEncoding() is called only if the
  * client has not already specified an encoding. By default, this
  * parameter is set to "true".
  *
  
  *
  *
  Although this filter can be used unchanged, it is also easy to
  * subclass it and make the selectEncoding() method more
  * intelligent about what encoding to choose, based on characteristics of
  * the incoming request (sUCh as the values of the Accept-Language
  * and User-Agent headers, or a value stashed in the current
  * user´s session.
  
  *
  * @author John Wong
  *
  * @version $Id: SetCharacterEncodingFilter.java,v 1.1 2002/04/10 13:59:27 johnwong Exp $
  */
  public class SetCharacterEncodingFilter implements Filter {
  
  // ------------------------ Instance Variables
  
  /**
  * The default character encoding to set for requests that pass through
  * this filter.
  */
  protected String encoding = null;
  
  /**
  * The filter configuration object we are associated with. If this value
  * is null, this filter instance is not currently configured.
  */
  protected FilterConfig filterConfig = null;
  
  /**
  * Should a character encoding specified by the client be ignored?
  */
  protected boolean ignore = true;
  
  
  // ---------------------Public Methods
  
  /**
  * Take this filter out of service.
  */
  public void destroy() {
  
  this.encoding = null;
  this.filterConfig = null;
  
  }
  
  /**
  * Select and set (if specified) the character encoding to be used to
  * interpret request parameters for this request.
  *
  * @param request The servlet request we are processing
  * @param result The servlet response we are creating
  * @param chain The filter chain we are processing
  *
  * @exception IOException if an input/output error occurs
  * @exception ServletException if a servlet error occurs
  */
  public void doFilter(ServletRequest request, ServletResponse response,
  FilterChain chain)
  throws IOException, ServletException {
  
  // Conditionally select and set the character encoding to be used
  if (ignore (request.getCharacterEncoding() == null)) {
  String encoding = selectEncoding(request);
  if (encoding != null)
  request.setCharacterEncoding(encoding);
  }
  
  // Pass control on to the next

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久久中精品2020中文 | 九草在线 | 天堂成人国产精品一区 | 亚洲成人福利在线 | 亚洲日色| 精精国产xxxx视频在线野外 | 日韩三区视频 | 香蕉成人在线视频 | 国产一区二精品区在线 | 少妇色诱麻豆色哟哟 | 国产免费一级 | xx53xx| 亚洲天堂在线电影 | 久久免费视频5 | 亚洲精品在线观看网站 | 一级精品 | 97风流梦电影 | 成人福利视频在线 | 久草在线资源观看 | 欧美成人一区二区三区 | 色污视频 | 欧美顶级毛片在线播放小说 | 午夜久久久精品一区二区三区 | 亚洲日本欧美 | 亚洲国产精品久久久久久久久久久 | 91国在线高清视频 | 欧美人与禽性xxxxx杂性 | 中文字幕一区在线观看视频 | 国产在线中文 | www久久国产| 免费在线观看国产精品 | 国产一级免费在线视频 | 特大黑人videos与另类娇小 | 欧美a欧美 | 精品国产一区二区三区成人影院 | 蜜桃网在线观看 | 久章草影院 | 密室逃脱第一季免费观看完整在线 | 亚洲尻逼视频 | 亚洲精久| 男女羞羞在线观看 |