[Hibernate + spring learning] s2sh integration notes-Chinese garbled characters

Source: Internet
Author: User

SSH2 + MySQL Chinese garbled solution (unified use of UTF-8 code)

Chinese garbled characters must be distinguished firstPage garbled, Action garbled, or database 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.

 

<% @ Pagecontenttype = "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 the JSP + JavaBean + servlet solution is used, Chinese garbled characters can be well solved. Set it 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 a filter in Web. xml [in the struts2 framework, this filter must be placed before the action filter! Order is very important!]

<! -ZH-CN encoding->
<Filter>
  <Filter-Name> encodingfilter </filter-Name>
  <Filter-class> com. sterning. Filter. encodingfilter </filter-class>
  <Init-param>
   <Param-Name> encoding </param-Name>
   <Param-value> UTF-8 </param-value> <! -- Or the encoding format you need -->
  </Init-param>
 </Filter>
 <Filter-mapping>
  <Filter-Name> encodingfilter </filter-Name>
  <URL-pattern>/* </url-pattern>
 </Filter-mapping>

Similar to the preceding 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 webserver 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 tomcateconnector character is involved here.

<Connectorport = "80" maxhttpheadersize = "8192 ″
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.

For 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;

}

Finally, set the database:

Take mysql5 as an example. When creating dB and table, set charset to GBK!

 

Summary:

In short, you must ensure that your character along the way to use the same encoding, preferably UTF-8.

The displayed JSP page must be <% @ page contenttype = "text/html; charset = UTF-8" Language = "Java" %>

In struts2, set <constant name = "struts. i18n. encoding" value = "UTF-8"/>

Write a filter to filter characters, also use UTF-8

Database MySQL creates a library encoded with a UTF-8

Note:

When Hibernate is configured in spring 2 (MySQL database), character encoding must be specified for the connection string; otherwise, it is absolutely garbled. For example, mine:

<Bean id = "datasource"
  Class = "org. springframework. JDBC. datasource. drivermanagerdatasource">
  <Propertyname = "driverclassname"
   Value = "org. gjt. Mm. MySQL. Driver">
  </Property>
  <Propertyname = "url" value = "JDBC: mysql: // localhost: 3306/mydata? Useunicode = true & amp; characterencoding = UTF-8 "> </property>
  <Propertyname = "username" value = "root"> </property>
  <Propertyname = "password" value = "root"> </property>
 </Bean>

The red part above is "& amp;", and cannot be written.

This is the result.

Address: http://blog.sina.com.cn/s/blog_4cc1f4390100b6gt.html

Thank you!

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.