The best solution to Garbled text:
1. Set the database encoding method to UTF-8.
2. The default encoding method of struts2 is UTF-8. That is struts. i18n. encoding = UTF-8.
3. Set the jsp page encoding method to UTF-8.
This eliminates the need to convert or filter characters each time:
1. character encoding filters.
1.1 set character encoding in a custom filter mode
Configuration in web. xml
<! -- Configure the struts2 filter -->
<Filter>
<Filter-name> struts2.x </filter-name>
<Filter-class> org. apache. struts2.dispatcher. FilterDispatcher </filter-class>
</Filter>
<! -- Configure the struts2 Chinese encoding filter -->
<Filter>
<Filter-name> CharacterEncoding </filter-name>
<Filter-class> com. cs. tb. util. CharacterEncodingFilter </filter-class>
<Init-param>
<Param-name> encoding </param-name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>
Package com. cs. tb. util;
Import java. io. IOException;
Import javax. servlet. Filter;
Import javax. servlet. FilterChain;
Import javax. servlet. FilterConfig;
Import javax. servlet. ServletException;
Import javax. servlet. ServletRequest;
Import javax. servlet. ServletResponse;
/**
* It is used to set the HTTP request character encoding filter. The filter parameter encoding is used to specify the character encoding used to process the Chinese characters of the Html Form request parameters.
*/
Public class CharacterEncodingFilter implements Filter {
Private FilterConfig filterConfig;
Private String encoding = "";
Public void destroy (){
FilterConfig = null;
Encoding = null;
}
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
If (encoding! = Null ){
Request. setCharacterEncoding (encoding); // sets the character encoding.
Chain. doFilter (request, response); // redirects requests and responses to the next link
}
}
Public void init (FilterConfig filterConfig) throws ServletException {
This. filterConfig = filterConfig;
This. encoding = this. filterConfig. getInitParameter ("encoding"); // obtain the initialization value encoding in the filter in the web. xml file.
}
}
1.2 use ActionContextCleanUp
Configuration in web. xml
<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>
2. request. setCharacterEncoding ("UTF-8 ");
3. str = new String (str. getBytes ("ISO8859-1"), "UTF-8 ");