Structs2 Chinese garbled Solution

Source: Internet
Author: User

Structs2 Chinese garbled Solution
To sum up, Chinese characters are garbled. First, you must identify whether the page is garbled, the action is garbled, or the database is garbled. The general principle is that java uses unicode encoding --> window uses gbk (gb2312 Extension Set) -- mysql uses UTF-8 by default (a unicode encoding method), so the escape will be garbled. The solution is as follows: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. It is worth noting that, if it is a jsp + java bean + servlet solution, Chinese garbled characters can be well solved, and it can be set to gb2312. 1.2 if you use the struts framework character set, you cannot set it to gb2312. You must change it to UTF-8. 2. Add the following in struts. properties:

Struts. devMode = false
Struts. enable. DynamicMethodInvocation = true
Struts. i18n. reload = true
Struts. ui. theme = simple

Struts. locale = zh_CN
Struts. i18n. encoding = UTF-8

Struts. serve. static. browserCache = false
Struts. url. includeParams = none

Locale and encoding are the character set.

3.Add filter in web. xml



Struts-cleanup

Org. apache. struts2.dispatcher. ActionContextCleanUp



Struts-cleanup
/*

  Similar to the above method, you can also set character series in action.


HttpServletResponse response = null;
Response = ServletActionContext. getResponse ();
Request. setCharacterEncoding ("UTF-8 ");
Response. setContentType ("text/html; charset = UTF-8 ");

Through the above method, we can basically solve the problem of Chinese garbled characters. Of course, there are also exceptions (such as the web server version and database version ). Similar to a Chinese Garbled text in one of my projects, tomcate5.5 is garbled, but not in tomcate6. The setting of the tomcctor ate character is involved here.

MaxThreads = "150" minSpareThreads = "25" maxSpareThreads = "75"
EnableLookups = "false" redirectPort = "8443" acceptCount = "100"
ConnectionTimeout = "20000" disableUploadTimeout = "true" URIEncoding = "GBK" />

--------------------------------------------------------------------

Note: When struts2 is used, garbled characters are still encountered. Later debugging discovered that,The web. xml configuration of struts2 is ordered..

The position of EncodingFilter in web. xml should be before FilterDispatcher in Struts2, because you must adjust the character set and then enter Action.

According to the Struts2 API, the filter order is
Struts-cleanup filter
SiteMesh filter
FilterDispatcher

--------------------------------------------------------------------

NOTE 2: This method is the best practice. It is used only when the previous method is invalid.

When request. getParameter () is directly used in the action, garbled characters still occur. Cause analysis:

1. getParameter () is a string parameter. Example:

String s = (String) request. getParameter ("txt ").GetBytes ("iso-8859-1 ");

2. String can also contain character parameters.

String(byte[] bytes, String charsetName)
Construct a newStringTo decode the specified byte array using the specified character set.

Example: String s = new String ("Chinese", "UTF-8 ");

3. Compile a class based on the above two points to complete this task.

Public class ConvertCharacter {

Public String Convert (String s ){

StringResult;

Byte [] temp;

Try {

Temp = s. getBytes ("iso-8859-1 ");

Result =New String (temp, "UTF-8 ");

}

Return result;

}

}

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.