"Struts Chinese problem", "struts internationalization problem"--Ultimate solution

Source: Internet
Author: User
Tags locale
Citation
----------------------------------------
----------------------------------------
To tell you the truth, your methods are all complicated, Java itself support multi-language coding, do not need to write any program, can be very simple
Realize.

The secret is two points:

1, all html/jsp pages using UTF-8 code

2, the client browser fully supports UTF-8 encoding

Steps:
1, first of all the html/jsp contenttype are set to UTF-8

2, then for the JSP program in non-ASCII code hint information should not be written in the program, should be placed in
Application.properties inside the unified management.

3, the HTML with the Native2ascii tool to do a unified processing, the HTML in the non-ASCII code are converted 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 such, and then to these configuration letters
The document is also processed once with the Native2ascii tool, converting non-ASCII code to Unicode encoding.

5, in the servlet request.getcharacterencoding () to obtain the client's operating system default encoding, and then set to struts
In the locale of httpsession.

Ok. Now different client visits will show different language versions. You can look at this time in your browser's character set, which is
UTF-8. Now your site is the same as Google, hey, in fact, you have a heart, look at your browser to visit Google is
What character set?

Remember: All html/jsp must be set to UTF-8 encoding, and non-ASCII characters in all files should be transferred using the Native2ascii tool
is a Unicode encoding that is represented in ASCII.
----------------------------------------
----------------------------------------
Original
----------------------------------------
The above mentioned is I from the Internet under a Chinese problem solution, to be exact, it should be about the internationalization of struts, and I will talk about how to achieve the internationalization of struts in my practice, I am not very proficient in theory, I can only completely by my own understanding and practice to tell, So the contents below may not be very correct, please forgive us. But one thing is for sure, I resolved the Chinese problem of struts through my own efforts, and achieved the internationalization of struts, in fact, it is not complicated, the following is the specific steps:

0. Problems encountered (these problems may not occur at the same time)
A. Chinese data from the database into the JSP has become a "????"
B. Do a good Chinese properties file, where the Chinese value in the page display garbled
c.jsp file in Chinese to browser after the display is garbled (we do not recommend in the JSP file input Chinese, as far as possible in the properties file)
D. The Chinese value from the JSP to the bean, and then the bean returns the page is garbled
E. When you change the language options for your local browser, the Web application does not automatically select the appropriate *.properties file based on your locale. Causes the Web application to be internationalized.
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 to the beginning of

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%>
Then set

3. Then edit the two *.properties files, put them in the Classes folder where you specify, here is placed under/web-inf/classes/com/wiley, they are:

Applicationresources.properties (English resource file)
Applicationresources_zh.properties (Chinese resource file)
You can write whatever tools you want.
4. Turn the applicationresources_zh.properties into gb2312. The above quotation said to turn into UTF-8, the result I tried, no. Turn into a gb2312 on the line, the operation is.
Rename the applicationresources_zh.properties to Applicationresources_xx.properties
Enter the Applicationresources_xx.properties folder on the DOS command line
Using commands: native2ascii-encoding gb2312 applicationresources_xx.properties applicationresources_zh.properties (As for why you appear "Native2ascii is not an internal command", please check other information, you may want to set the environment variable, because he is the JDK folder bin under an application)
5. The next configuration struts-config.xml, very simple, we add:

<message-resources parameter= "Com.wiley.ApplicationResources"/> on the line;

The

    has been able to solve most of the Chinese problems. As mentioned above A,b,e now open the browser, select the menu: Tools Internet Options Language, "Chinese-China [ZH-CN]" deleted, add an "English-UK [ZH-GB]" After the confirmation, restart Tomcat, enter the URL you will find that, The text information on your page will use the contents of the Applicationresources.properties   (English resource file). If you change back to "Chinese-China [ZH-CN]", it will display the Chinese content in the Applicationresources_zh.properties (Chinese resource file).

  As for the problem "c.jsp file in Chinese to browser after the display is also garbled" you will use the 4th step similar method to the *.jsp file encoding, then-encoding parameters will be used UTF-8, if you are using Struts Studio 5.2 Pro   For  eclipse tools, this step is free. It is automatically stored in a UTF-8 format.
  As to the problem "D. The Chinese value from the JSP to the bean, and then the Bean returned to the page is garbled" solution, I just added a filter.
You can add in Web.xml now:
<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>

And then add a Java file to the package you specified. I put it in the/web-inf/classes/com/wiley, the following 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;

/**
* <p>filter that sets the character encoding-to is used in parsing the
* Incoming request, either unconditionally or only if the client did not
* Specify a character encoding. Configuration of this filter was 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 a encoding. By default, this
* parameter is set to "true" .</li>
* </ul>
*
* <p>although This filter can be used unchanged, it's also easy to
* Subclass it and make the <code>selectencoding () </code>
* 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 this pass through
* This filter.
*/
protected String encoding = NULL;


/**
* The filter configuration object we are associated with. If This value
* Is NULL, this filter instance isn't currently configured.
*/
protected Filterconfig filterconfig = null;


/**
* Should a character encoding specified by the client to 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 is 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 in 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 is used, based on the
* Characteristics of the current request and/or filter initialization
* Parameters. If no character encoding should is 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
The Chinese problems I encountered have all been solved, and I understand the deep meaning of the internationalization of struts.
I personally feel that struts as a powerful application framework should have taken into account its internationalization problems, and in practical applications will not be very complicated, as long as we follow some rules, we can enjoy the endless fun struts brings us. I hope the above mentioned is helpful to everyone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.