A application that supports I18n should have some features: 1 When the support is added, it is required to change the program code 2 character element, message, and the image save in the original code 3 dependent on different cultural data, such as: date time, decimal Data such as cash symbols should have a correct format 4 application to quickly adapt to new language and / or new regions Struts mainly use two I18n components to implement international programming: The first component is A message class managed by the application controller, which references the resource package that contains regional information strings. The second component is a JSP custom label,
It is used to present the actual string of controlled managers in the View layer. The contents of these two aspects in our previous login examples have appeared. The standard practice of implementing international programming with Struts is: Generating a Java property file set. Each file contains the key / value pairs of all messages to display. Naming of these files Comply with the following rules, the file representing the English message can be used as the default file, its name is ApplicationResources.properties; other language files are in the file name to bring the corresponding region and language code string, such as The file name representing Chinese should be ApplicationResources_n_cn.properties. And other language files are placed in the same directory with the ApplicationResources.properties file. The key / value of the ApplicationResources.properties file is in English, while the keys of other language files are in English, and the values are the corresponding language. As in the login example of our login example: logon.jsp.prompt.username = username: Logon.jsp.prompt.userName = User name: Of course, in the actual application, Chinese Convert to ASCII code. With the previous article and some of the basic knowledge introduced above. We can make our login program to international programming. First, all of our JSP page files are set to UTF-8. That is, the following instructions are written in the page file line: <% @ Page ContentType = "text / html; charset = UTF-8"%>, in our login example, there is no need to change again. Second, put all the REQUEST character sets are also set to UTF-8. Although we can add such sentences in each file: Request.SetCharacterencoding ("UTF-8"); to solve it, but this is very troublesome. A simpler solution is to use Filter. The specific steps are as follows: In the MyStruts / Web-INF / CLASES directory, create a directory called Filters, create a new class named: setCharacterencodingFilter and save it in this directory. In fact, this class does not want you to write, you can borrow the example in Tomcat. The program selection of this example is as follows:
Package filters;
Import java.io.ioException;
Import javax.servlet.filter;
Import javax.servlet.filterchain;
Import javax.servlet.filterconfig;
Import javax.servlet.servletException;
Import javax.servlet.servletRequest;
Import javax.servlet.servletResponse;
Import javax.servlet.unavailableException;
/ **
*
eXample filter tria set the character encoding to be used in Parsing the * incoming request, Either Unconditionally or Only The Client Did NOT
* Specify a character encoding. Configuration of this filter is based ON
* The Following Initialization Parameters: P>
*
*
* for this request, Either conditionally or unconditionally based ON
* The ignore code> Initialization Parameter. this parameter
* is Required, soled, sore is no default. li>
*
* specified by the client is ignored, and the value returned by THE
* selectEncoding () code> method is set. if set to "false,
* selectEncoding () code> is caled only strong> if the
* Client Has Not Already Specified An Encoding. by Default, THIS
* Parameter is set to "true". li>
* ul>
*
*
Althought, IT IS Also Easy To
* Subclass it and make the selectencoding () code> Method More
* Intelligent About What Encoding to Choose, Based On Characteristics of
* The incoming request (Such as the value of the value accept-language code>
* And user-agent code> Headers, or a value stashed in the capital
* User's session. p>
*
* @Author craig mcclanahan
* @version $ revision: 1.2 $ $ date: 2001/10/17 22:53:19 $
* /
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 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 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 code>.
*
* The default importation unconditionally returns the value configured
* by the Encoding strong> Initialization Parameter for THIS
* Filter.
*
* @Param Request The Servlet Request WE Are Processing
* /
protected string selectencoding (servletRequest request) {
Return (this.encoding);
}
}
Among them, Request.SetCharacterencoding (Encoding); is a key sentence. In order to make this class, we also have to configure it in the web.xml file, the configuration code is as follows:
init-param>
filter>
filter-mapping>
Finally, it is to prepare a resource package file. We use the Chinese file as an example: Open the ApplicationResources.properties file, saved as ApplicationResources_zh.properties, which is just a transitional file. The value of the file mile / value pair is expressed in Chinese. The code after the change is as follows: #application resource for the logon.jsp
Logon.jsp.title = login page
Logon.jsp.page.Heading = Welcome to the world!
Logon.jsp.prompt.username = Username:
Logon.jsp.prompt.password = password:
Logon.jsp.prompt.submit = Submit
Logon.jsp.prompt.reset = reset
#Application resource for the main.jsp
Main.jsp.title = Home
Main.jsp.welcome = Welcome:
#Application resource for the logonaction.java
Error.Missing.userName =
Error.Missing.password =
#Application resource for the userinfobo.java
Error.Nomatch =
#Application resource for the userinfobo.java
Error.logon.invalid =
Error.Removed.user =
Error.unexpected =
Use the Native2ASCII tool to convert Chinese characters in the above file to ASCII, and generate a final resource file ApplicationResources_zh_cn.properties. The specific approach is to open a dos window to the next mystruts / WEB-INF / classes directory, run the following statement: content native2ascii -encoding GBK ApplicationResources_zh.properties ApplicationResources_zh_CN.properties generated file ApplicationResources_zh_CN.properties as follows:
#Application resource for the logon.jsp
Logon.jsp.title = / u767b / u5f55 / u9875
Logon.jsp.page.Heading = / u6b22 / u8fce / u4e16 / u754c!
Logon.jsp.prompt.username = / u7528 / u6237 / u540d: logon.jsp.prompt.password = / u53e3 / u4ee4:
Logon.jsp.prompt.submit = / u63d0 / u4ea4
Logon.jsp.prompt.reset = / u590d / u4f4d
#Application resource for the main.jsp
Main.jsp.title = / u4e3b / u9875
Main.jsp.welcome = / u6b22 / u8fce:
#Application resource for the logonaction.java
Error.Missing.username =
Error.Missing.password =
#Application resource for the userinfobo.java
Error.Nomatch =
#Application resource for the userinfobo.java
Error.logon.invalid =
Error.Removed.user =
Error.unexpected =
As you can see here, all Chinese characters are converted into a corresponding Unicode code. Now, run the login example program, you will find it is already displayed. In the "Tools" - "Internet Options" of the browser, remove "Chinese (China)" plus English, try again, at this time, in English. This is to say that customers of different countries (regions) can see the content of their own language, which achieves the basic requirements of international programming. If you want to display other languages, you can use a similar processing Chinese method, which is not well known. The database used in the example program in this article is still MS SQLServer2000, and the database character set is GBK. Experiments have shown that they can support simple, traditional Chinese, English and Japanese characters.