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 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 |
where locale, encoding is the setting of the character set.
3. Add a filter to Web. XML
<!--ZH-CN Encoding-- <filter> <filter-name>struts-cleanup</filter-name> <filter-class> Org.apache.struts2.dispatcher.ActionContextCleanUp </filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
|
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"); &NBSP; |
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
Filterdispatcher
--------------------------------------------------------------------
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; try{ < c21> Temp = s.getbytes ("iso-8859-1" ); > result = new String (temp, "utf-8"); < c26> } < c37> return result; } } |
STRUCTS2 Chinese garbled solution