Struts Internationalization - Ultimate Solution

zhaozj2021-02-16  124

Quit

----------------------------------------

----------------------------------------

To be honest, your method is complex, Java itself supports multi-language code, no need to write any procedures, it can be very simple

achieve.

The secret is two points:

1, all HTML / JSP pages are all UTF-8 encoding

2, client browser fully supports UTF-8 encoding

step:

1. First, all HTML / JSP contentType is set to UTF-8

2, then do not write in the program for non-ASCII code prompt in the JSP program, should be put

Connect in Application.properties.

3. Do HTML with the Native2ASCII tool unified to handle, convert the non-ASCII code in HTML to Unicode encoding.

4. For different languages, write different Application.Properties, such as Simplified Chinese is

Application_zh_cn.properties, Traditional Chinese is Application_ZH_TW.Properties, then these configuration letters

The interest file is also handled once with the Native2ASCII tool, and the non-ASCII chi-Book is converted to Unicode encoding.

5, at the servlet Request.getCharacterencoding () get the client's operating system default encoding, then set to Struts

HttpSession's locale.

OK! Different customer visits are now displayed different languages. You can look at the character set of your browser at this time, that is

UTF-8. Now your website is like Google, hey, in fact, if you have a heart, see if your browser visits Google is

What kind of character set

Remember: All HTML / JSPs are set to UTF-8 encoding, and the non-ASCII code characters in all files must be turned with Native2ASCII tools.

In order to use the Unicode encoding represented by ASCII.

-------------------------------------------------- ------------------------------

Original

----------------------------------------

The above is a solution from the Chinese problem. It is exactly that it should be about the internationalization of Struts. Here I combine my practice talk about how to implement the internationalization of Struts, I am the theory Not very proficient, I can only tell the understanding and practice, so the content below may not be very correct, please forgive. But one thing can be sure, I solved the Chinese problem of Struts through my own efforts, and realize the internationalization of Struts. In fact, everything is not complex, the following is the specific step:

0. The problem encountered (these issues may not appear at the same time)

a. Chinese data becomes "????" from the database to the JSP

b. Do a good Chinese Properties file, where Chinese Value displays garbled on the page

The Chinese to the browser in the C.Jsp file is also garbled when it is displayed (it is recommended not to enter Chinese in the JSP file, try to put it in the Properties file)

d. By the JSP passed to the Chinese value, and then back the page back to the page is garbled.

e. When replacing the language options of the local browser, the web application cannot automatically select the appropriate * .properties file according to your LOCALE. Leading web applications cannot be international.

Environment:

Web server: Tomcat 5.0.19

Operating system: Win2000 Server

JVM: JDK 1.4

Database: Oracle 8.1.7

Development Tools: Struts Studio 5.2 Pro for Eclipse

2. Join all the * .jsp webpage first

<% @ Page language = "java" contenttype = "text / html; charset = UTF-8"%> Set

3. Then edit the two * .properties files, place them under the Classes folder, here is placed in / web-inf / class / com / wiley, they are:

ApplicationResources.properties (English resource file)

ApplicationResources_zh.properties (Chinese resource file)

Just use what tools are written!

4. Transcode ApplicationResources_ZH.Properties into GB2312. The above citation said to be transferred into UTF-8, and I tried it, no. It is possible to turn into GB2312, and the operation is.

Rename ApplicationResources_ZH.Properties to ApplicationResources_xx.properties

Enter the folder where the DOS command line enters ApplicationResources_xx.properties

Use command: native2ascii -Encoding GB2312 ApplicationResources_xx.properties ApplicationResources_zh.properties (as for "Native2ASCII is not internal command", please check other information, maybe you have to set the environment variable, because he is a JDK folder Bin application)

5. Configure Struts-Config.xml next to it, it is very simple, we join:

This has been able to solve most Chinese issues. As mentioned above, the A, B, E now open the browser, select the menu: Tool "Internet Option" language, "Chinese-China [ZH-CN]" delete, add a "English-British [zh-gb] "After confirming, restart Tomcat, enter the URL. You will find that your page text information will be used in the ApplicationResources.properties (English resource file). If you change back, "Chinese-China [EN]", it will display the Chinese content in the ApplicationResources_zh.properties (Chinese resource file).

As for problems "C. JSP files are also garbled when they are displayed after the browser" "You have to use a similar method similar to the fourth step to the * .jsp file encoding, then -Encoding parameters will use UTF- 8. If you use the Struts Studio 5.2 Pro for Eclipse tool, this step is free. It automatically stores the format of UTF-8.

As for the problem "D. The Chinese value passed by JSP to bean, and then it is solved by the bean transfer to the page again", I just add a filter.

You can now join in Web.xml:

Set Character Encoding

com.wiley.setcharacterencodingfilter

Encoding UTF-8

ignore

True

Set Character Encoding

Action

Then add a java file in your designated package I am in / web-inf / classs / com / wiley, below the source code:

/ *

* 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 what sets the character encoding to be buy in paarsing 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, soled, sore 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 caled only if the

    * Client Has Not Already Specified An Encoding. By Default, THIS

    * Parameter is set to "true".

    *

    *

    *

    Althought, 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 value of the value accept-language

    * And user-agent Headers, or a value stashed in the capital

    * User's session.

    *

    * @Author John Wong

    *

    * @Version $ ID: setcharacterencodingfilter.java, v 1.1 2002/04/10 13:59:27 Johnwong Exp $

    * /

    Public class setcharacterencodingfilter imports 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 filter firmconfig = 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 @ @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 available

    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 shop be set, return

    * null .

    *

    * The default importation unconditionally returns the value configured

    * By the Encoding Initialization Parameter for THIS

    * Filter.

    *

    * @Param Request The Servlet Request WE Are Processing

    * /

    Protected String SelectEncoding (ServletRequest Request) {Return (this.Encoding);

    }

    } // eoc

    It is all solved by the Chinese issue I have learned here and understand the deep meaning of the internationalization of Struts.

    I personally think that Struts as a powerful application framework should take into account its internationalization, and it will not be very complicated in practical applications. As long as we follow some rules, we can enjoy Struts to bring us. pleasure. I hope that the above will help you.

转载请注明原文地址:https://www.9cbs.com/read-9130.html

New Post(0)