"Struts Chinese Problem", "Struts Internationalization Problem" Ultimate Solution (Transfer)

xiaoxiao2021-03-06  88

Ultimate Solution of "Struts Chinese", "Struts Internationalization"

Java itself supports multi-language coding. You don't need to write any procedures, you can make it easy. The secret is two points: 1, all HTML / JSP pages are all UTF-8 encoding 2, the client browser fully supports the UTF-8 encoding step: 1, first set all the HTML / JSP's ContentType to UTF-8 2 , Then do not write in the program for the non-ASCII code prompt information in the JSP program, should be managed 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 use the Native 12ASCII tool to handle it once, turn non-ASCII chimdom Encoded for Unicode. 5. Get the client's operating system default encoding, then set to the httpsession of the Struts. OK! Different customer visits are now displayed different languages. You can look at the character set of your browser at this time, is UTF-8. Now your website is like Google, hey, in fact, you have a word, look at what is your browser to visit Google, what is the character set: All HTML / JSP is set to UTF-8 encoding, all files Non-ASCII code characters must be converted to Unicode encoding represented by ASCII with Native2ASCII tools.

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, it 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 "????" b. Do "????" file, the Chinese property file, where the Chinese Value displays Chinese to the browser in garbled C.JSP files on the page It 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 of the bean, then transferred to the page back to the page is also garbled E. When replacing the local browser language When the option, the web application cannot automatically select the appropriate * .properties file according to your LOCALE. Leading web applications cannot be international. 1. 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. First Add All * .JSP Pages 3. Then edit the two * .properties files, place it under the Classes folder, here is placed under / web-inf / class / com / wiley, they are: ApplicationResources.Properties (English) ApplicationResources_zh.properties (Chinese resource file) Waste what tool is 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. The ApplicationResources_zh.properties renamed ApplicationResources_xx.properties in the DOS command line enter the folder where the file ApplicationResources_xx.properties use the command: native2ascii -encoding gb2312 ApplicationResources_xx.properties ApplicationResources_zh.properties (As to why you will "native2ascii is not an internal command" ,, please check Other information, you may set an environment variable because he is an application under the JDK folder bin) 5. Next to configure struts-config.xml, it is very simple, we join: OK; you can solve it. 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. 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: set character encoding com.wiley.setcharacterencodingfilter Encoding UTF-8 Ignore True Set Character Encoding Action then adds a Java file in your designated package I put it in / web-inf / classes / com / wiley, below is 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 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 * selectedEncoding () 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 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 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 REQU est 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 (required);

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

New Post(0)