Citation
----------------------------------------
----------------------------------------
To be honest, all your methods are complicated. Java itself supports multi-language coding and does not need to write any program. It can be very simple.
.
There are two tips:
1. All html/JSP pages are UTF-8 encoded
2. The client browser fully supports UTF-8 Encoding
Steps:
1. First of all the HTML/jsp contenttype are set to UTF-8
2. The non-ASCII code prompts In the JSP program should not be written in the program.
Unified management in application. properties.
3. Use native2ascii tools to process HTML in a unified manner and convert non-ASCII codes in HTML to unicode encoding.
4. Write different application. properties for different languages. For example, the simplified Chinese is
Application_zh_cn.properties. The traditional Chinese is application_zh_tw.properties.
The native2ascii tool is also used to process all the non-ASCII codes.
5. In Servlet request. getcharacterencoding (), obtain the default Operating System encoding of the client, and then set it to struts.
In the locale of httpsession.
OK! Now, different customer accesses will display different language versions. You can see that the character set of your browser is
UTF-8. Now your website is the same as Google. Hey hey, if you are interested, check whether your browser accesses Google.
What character set?
Remember: All html/jsp files must be set to UTF-8 encoding, and all non-ASCII characters in files must be converted to native2ascii
It is a unicode code in ASCII format.
----------------------------------------
----------------------------------------
Original
----------------------------------------
The above is a solution to Chinese problems I have posted on the Internet. Specifically, it should be about struts's internationalization, next I will talk about how to implement struts internationalization based on my practice. I am not very proficient in theory. I can only talk about it based on my own understanding and practice, the content below may not be very correct. Please forgive me. However, it is certain that I have solved struts's Chinese problem through my own efforts and achieved struts internationalization. In fact, everything is not complicated. The specific steps are as follows:
0. Problems Encountered (these problems may not occur at the same time)
A. After the Chinese data is transferred from the database to the JSP, it becomes "???? "
B. The Chinese properties file is prepared, and the Chinese value is garbled on the page.
Chinese characters in the c. jsp file are garbled when displayed in the browser. (It is recommended that you do not enter Chinese characters in the JSP file and try to put them in the properties file)
D. The Chinese value sent from JSP to bean, and then the bean returns the page with garbled characters
E. When the language option of the local browser is changed, the Web application cannot automatically select the appropriate *. properties file based on your locale. As a result, web applications cannot be internationalized.
1. Environment:
Web server: Tomcat 5.0.19
Operating System: Win2000 Server
JVM: JDK 1.4
Data Base: Oracle 8.1.7
Development Tool: struts studio 5.2 pro for eclipse
2. Add all the headers in the *. jsp webpage first
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" %>
Set <HTML: HTML locale = "true">
3. Edit the two *. properties files and place them in the classes folder. Put them in the/Web-INF/classes/COM/Wiley directory. They are:
Applicationresources. properties (English resource file)
Applicationresources_zh.properties (Chinese resource file)
You can write it with any tool!
4. transcode applicationresources_zh.properties to gb2312. The above citation said to turn into a UTF-8, the result I tried, no. Convert it to gb2312. The operation is.
Change applicationresources_zh.properties to applicationresources_xx.properties.
Enter the folder where applicationresources_xx.properties is located in the doscommand line.
Run the following command: native2ascii-encoding gb2312 applicationresources_xx.properties applicationresources_zh.properties (for why "native2ascii is not an internal command" appears, check other materials. You may need to set environment variables, because it is an application in the JDK folder bin)
5. Next configure the struts-config.xml, very simple, we add:
<Message-resources parameter = "com. Wiley. applicationresources"/>;
This can solve most Chinese problems. As mentioned above, a, B, and E open the browser, select the menu: Tools, Internet Options, and delete "Chinese-China [zh-cn, after "English-UK [zh-GB]" is added, restart tomcat. Enter the URL and you will find that the text on your page uses applicationresources. properties (English resource file. If you switch back to "Chinese-China [zh-cn]", it will display the Chinese content in applicationresources_zh.properties (Chinese resource file.
As for the question "C. chinese characters in JSP files are garbled when they are displayed in the browser. "You need to use a method similar to Step 1 to repeat *. JSP file encoding, then-encoding parameters will use the UTF-8, if you are also using struts studio 5.2 pro for eclipse tool, this step is free. It is automatically stored in the UTF-8 format.
As for the problem "D. It is garbled again to send the Chinese value from JSP to bean and then return the page from Bean", I just added a filter.
You can add the following content to 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>
Then add a Java file to the package you specified and put it in/Web-INF/classes/COM/Wiley. The source code is as follows:
/*
* 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
* 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
* <Code> selectencoding () </code> method is set. If set to "false,
* <Code> selectencoding () </code> is called <strong> only </strong> If
* 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
* Subclass it and make the <code> selectencoding () </code> method more
* Intelligent about what encoding to choose, based on characteristics
* 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 = NULL;
/**
* Shoshould 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
* 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) throws servletexception {
This. filterconfig = filterconfig;
This. Encoding = filterconfig. getinitparameter ("encoding ");
String value = filterconfig. getinitparameter ("Ignore ");
If (value = NULL)
This. Ignore = true;
Else if (value. inclusignorecase ("true "))
This. Ignore = true;
Else if (value. inclusignorecase ("yes "))
This. Ignore = true;
Else
This. Ignore = false;
}
// ---------------------------------------------------------- Protected Methods
/**
* Select an appropriate character encoding to be used, based on
* Characteristics of the current request and/or filter Initialization
* Parameters. If no character encoding shoshould 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
All the Chinese problems I encountered have been solved, and I understand the profound meaning of struts internationalization.
I personally think that struts, as a powerful application framework, should have taken into account its internationalization problems for a long time and will not be very complicated in practical applications. As long as we follow some rules, you can enjoy the endless fun that struts brings to us. I hope the above will help you.