STRUCTS2 Chinese garbled solution

Source: Internet
Author: User
Tags i18n

Summarize, the Chinese garbled, first to distinguish is the page garbled, action garbled, or database garbled. The general principle is that Java uses Unicode encoding-->window to use GBK (an extension set of gb2312)--mysql The default use of Utf-8 (an encoding method of Unicode), so that the spin is garbled ^_^. Here's how to fix it:1. In Struts2, it is best to set all characters to Utf-8.
<%@ page contenttype= "text/html; Charset=utf-8 "%>
<%@ page pageencoding= "UTF-8"%>
1.1 Set the character encoding on the JSP page. Here there must be explained is if it is Jsp+java bean+servlet scheme, Chinese garbled very good solution, unified set to gb2312 on it.1.2 Using the Struts framework character set cannot be set to gb2312, to be changed to Utf-8.2. Add in




where locale, encoding is the setting of the character set.

3. Add a filter to Web. XML

<!--ZH-CN Encoding--

similar to the above method, there is also a character qualifier set in action.

      httpservletresponse response = null;
      response = Servletactioncontext.getresponse ();
      request.setcharacterencoding ("Utf-8");
      response.setcontenttype ("Text/html;charset=utf-8");


through the above method, the basic can solve the problem of Chinese garbled. Of course, there are exceptions (such as the version of Web server \ Database version, and so on). Like in one of my projects encountered a Chinese garbled, tomcate5.5 will be garbled, and in the Tomcate6 will not. This is related to the setting of the Tomcate connector character.

&NBSP;      <connector port=" "maxhttpheadersize=" 8192 "
                             maxthreads= "All" minsparethreads= "maxsparethreads="
                             enablelookups= "false" redirectport= "8443" acceptcount= "100"
                             connectiontimeout= "20000" disableuploadtimeout= "true"   uriencoding= "GBK"    />


One of the PostScript: When using STRUTS2, still encountered a garbled. Later debugging only found that thestruts2 Web. XML configuration is sequential .

the location of Encodingfilter in Web. XML should precede the Filterdispatcher of Struts2, because the character set is first adjusted and then moved to action.

The order of Api,filter according to Struts2 is
Struts-cleanup Filter
Sitemesh Filter


PostScript Two: This method is the next worst, only in the previous method is invalid when used.

The Request.getparameter () is used directly in the action, or garbled. The causes are analyzed as follows:

1, GetParameter () is a character parameter. Cases:

string s = (string) request.getparameter ("TXT"). getBytes ("iso-8859-1");

2, string can also have character parameters.

String(byte[] bytes, String charsetName)
Constructs a new Stringby decoding the specified byte array using the specified character set.

Example: string s = new string ("Chinese", "utf-8");

3, synthesis of the above two points, write a class to complete this task

public class convertcharacter{

public string Convert (string s) {

           String result;

          < c29> byte[] temp;


          < c21> Temp = s.getbytes ("iso-8859-1" );

            > result = new String (temp, "utf-8");

          < c26> }

          < c37> return result;



STRUCTS2 Chinese garbled solution

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: 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.