<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    jwethand

    Nothing is particularly hard if you divide it into small jobs.
    posts - 21, comments - 18, trackbacks - 0, articles - 20

    實現(xiàn)Struts的國際化,其實一切并不復(fù)雜,下面是具體步驟:

    0.遇到的問題(這些問題也許不會同時出現(xiàn))
    a.中文數(shù)據(jù)從數(shù)據(jù)庫中到j(luò)sp中后就變成了"????"
    b.做好的中文properties文件,其中的中文value在頁面顯示亂碼
    c.jsp文件中的中文到瀏覽器后顯示時也是亂碼(建議不要在jsp文件中輸入中文,盡量放在properties文件中)
    d.由jsp傳給bean的中文值,再由bean傳回頁面又是亂碼
    e.當更換本地瀏覽器的語言選項時,Web應(yīng)用程序不能自動根據(jù)你的locale選擇合適的*.properties文件。導(dǎo)致Web應(yīng)用程序不能國際化。

    1.環(huán)境:
    Web服務(wù)器: Tomcat 5.0.19
    操作系統(tǒng): Win2000 Server
    JVM : jdk 1.4
    數(shù) 據(jù) 庫: Oracle 8.1.7
    開發(fā)工具: struts studio 5.2 pro for eclipse

    2.先將所有*.jsp 網(wǎng)頁中開頭處加入
    <%@ page language="java" contentType="text/html; charset=utf-8" %>
    再設(shè)置<html:html locale = "true">

    3.然后編輯好兩個*.properties文件,放在classes文件夾下你指定的地方,這里是放在/web-inf/classes/com/wiley 下,它們分別是:
    ApplicationResources.properties (英文資源文件)
    ApplicationResources_zh.properties (中文資源文件)
    隨便用什么工具編寫都行啊!

    4.將ApplicationResources_zh.properties轉(zhuǎn)碼成gb2312。上面引文說要轉(zhuǎn)成UTF-8,結(jié)果我試了,不行。轉(zhuǎn)成gb2312就行了,操作是。
    將ApplicationResources_zh.properties更名為ApplicationResources_xx.properties
    在DOS命令行進入ApplicationResources_xx.properties所在的文件夾
    使用命令:native2ascii -encoding gb2312 ApplicationResources_xx.properties

    ApplicationResources_zh.properties(至于你為什么會出現(xiàn)“native2ascii不是內(nèi)部命令”,請查其它資料,可能你要設(shè)置環(huán)境變量,因為他是jdk的文件夾bin下的一個應(yīng)用程序)

    5.接下來配置struts-config.xml,很簡單,我們加入:
    <message-resources parameter="com.wiley.ApplicationResources"/> 就行了;

    到此已能解決大多數(shù)中文問題。如上面所說的a,b,e 現(xiàn)在打開瀏覽器,選擇菜單:工具》internet選項》

    語言,將“中文-中國[zh-cn]”刪掉,添加一個“英語-英國[zh-gb]”確定后,重啟Tomcat,輸入網(wǎng)址你就會發(fā)現(xiàn),你的頁面的文本信息就會用的是ApplicationResources.properties (英文資源文件)中的內(nèi)容。如果換回“中文-中國[zh-cn]”,它就會顯示ApplicationResources_zh.properties (中文資源文件)中的中文內(nèi)容。

    至于問題“c.jsp文件中的中文到瀏覽器后顯示時也是亂碼” 你就要用與第4步類似的方法來重新對*.jsp 文件編碼,這時-encoding的參數(shù)就要用UTF-8了,如果你用的也是struts studio 5.2 pro for eclipse工具,這一步就免了。它會自動用UTF-8的格式存儲。
    至于問題“d.由jsp傳給bean的中文值,再由bean傳回頁面又是亂碼”的解決,我只是加了個過濾器。
    你可以現(xiàn)在web.xml中加入:

    <filter>
    <filter-name>Set Character Encoding</filter-name>
    <filter-class>com.wiley.SetCharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>utf-8</param-value>
    </init-param>
    <init-param>
    <param-name>ignore</param-name>
    <param-value>true</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>Set Character Encoding</filter-name>
    <servlet-name>action</servlet-name>
    </filter-mapping>

    然后在你指定的包內(nèi)加個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;

    /**
    * <p>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:</p>
    * <ul>
    * <li><strong>encoding</strong> - The character encoding to be configured
    * for this request, either conditionally or unconditionally based on
    * the <code>ignore</code> initialization parameter. This parameter
    * is required, so there is no default.</li>
    * <li><strong>ignore</strong> - If set to "true", any character encoding
    * specified by the client is ignored, and the value returned by the
    * <code>selectEncoding()</code> method is set. If set to "false,
    * <code>selectEncoding()</code> is called <strong>only</strong> if the
    * client has not already specified an encoding. By default, this
    * parameter is set to "true".</li>
    * </ul>
    *
    * <p>Although this filter can be used unchanged, it is also easy to
    * subclass it and make the <code>selectEncoding()</code> method more
    * intelligent about what encoding to choose, based on characteristics of
    * the incoming request (such as the values of the <code>Accept-Language</code>
    * and <code>User-Agent</code> headers, or a value stashed in the current
    * user′s session.</p>
    *
    * @author <a href="mailto:jwtronics@yahoo.com">John Wong</a>
    *
    * @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 filter
    chain.doFilter(request, response);

    }


    /**
    * Place this filter into service.
    *
    * @param filterConfig The filter configuration object
    */
    public void init(FilterConfig filterConfig) throws ServletException {

    this.filterConfig = filterConfig;
    this.encoding = filterConfig.getInitParameter("encoding");
    String value = filterConfig.getInitParameter("ignore");
    if (value == null)
    this.ignore = true;
    else if (value.equalsIgnoreCase("true"))
    this.ignore = true;
    else if (value.equalsIgnoreCase("yes"))
    this.ignore = true;
    else
    this.ignore = false;

    }


    // ------------------------------------------------------ Protected Methods


    /**
    * Select an appropriate character encoding to be used, based on the
    * characteristics of the current request and/or filter initialization
    * parameters. If no character encoding should be set, return
    * <code>null</code>.
    * <p>
    * The default implementation unconditionally returns the value configured
    * by the <strong>encoding</strong> initialization parameter for this
    * filter.
    *
    * @param request The servlet request we are processing
    */
    protected String selectEncoding(ServletRequest request) {

    return (this.encoding);

    }

    }//EOC
    到此我遇到的中文問題已全部得到解決,并從中理解到struts的國際化的深刻含義。
    我個人覺得struts作為一個功能強大的應(yīng)用框架,應(yīng)該早就考慮到它的國際化問題,并在實際應(yīng)用中不會很復(fù)雜,只要我們遵循一些規(guī)則,就可以盡情享受struts給我們帶來的無窮樂趣。希望以上所述對大家有所幫助。??


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲午夜电影在线观看| 无人在线观看免费高清视频| 97视频免费观看2区| 午夜男人一级毛片免费| 亚洲人JIZZ日本人| 亚洲日本乱码卡2卡3卡新区| 一级做受视频免费是看美女| 亚洲免费电影网站| 久久精品国产精品亚洲艾草网美妙| 亚洲国产模特在线播放| 国产激情久久久久影院老熟女免费| 青娱乐免费在线视频| 亚洲最大AV网站在线观看| 亚洲一区二区三区在线观看网站| 十八禁在线观看视频播放免费| 精品国产免费观看| 久久精品亚洲一区二区三区浴池| 黄色毛片免费网站| 成人免费毛片内射美女-百度| 久久久久久久尹人综合网亚洲| 亚洲AV无码专区在线电影成人| 最近免费最新高清中文字幕韩国| 亚洲av无码成人精品区在线播放 | 一本色道久久综合亚洲精品蜜桃冫| 国产精品福利在线观看免费不卡| 性色av免费观看| 久久久久久亚洲精品成人| 国产一级黄片儿免费看| 全部免费国产潢色一级| 亚洲中文字幕乱码AV波多JI| 久久99精品视免费看| 亚洲日韩国产精品第一页一区| 在线视频亚洲一区| 在线免费视频一区| 亚洲综合色婷婷在线观看| 免费下载成人电影| 亚洲激情视频网站| 无码国产精品一区二区免费式直播| 亚洲AV无码一区二区乱孑伦AS| 国产精品无码永久免费888| 免费人妻av无码专区|