OFBiz 的中文乱码解决方案

OFBiz开发时遇到乱码的问题,MySQL数据库插入中文数据时出现了一个问题,报告中文错误:incorrect string value .....

1.这时由于字符编码不一致导致的,应该将数据库对应的表或coloum改成uft-8,所以如果在CustomerExtra这表中存储中文的话,需要将
  此表另外还有operationlog表的charset改成utf-8

2.Servlet中字符显示为证券的中文,而且表也设置为utf-8,但是数据库和页面还是乱码,怎么回事?
  这里需要修改jdbc url的参数,如将jdbc:mysql://localhost/ccbportal?zeroDateTimeBehavior=convertToNull
  改成:jdbc:mysql://localhost/ccbportal?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8

3.前台输入的是中文,到后台就变成了乱码,怎么回事?
  无论何种表单提交都可以在后台的java文件中通过String des = new String(s.getBytes("iso8859-1"),"UTF-8");
  来转换成你想要的UTF-8编码方式。但如果每处都加词句太麻烦,故分post和get两种方式区分提交。
  写一个Filter即可解决问题:
 


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  1 
 2  import java.io.IOException;
 3  import javax.servlet.ServletException;
 4  import javax.servlet.Filter;
 5  import javax.servlet.FilterChain;
 6  import javax.servlet.FilterConfig;
 7  
 8  import javax.servlet.ServletRequest;
 9  import javax.servlet.ServletResponse;
10  
11  public class SetCharacterEncodingFilter implements Filter {
12  
13   protected String encoding = "GBK";
14  
15   protected FilterConfig filterConfig = null;
16  
17   protected boolean ignore = true;
18  
19   public void init(FilterConfig filterConfig) throws ServletException {
20    this.filterConfig = filterConfig;
21    this.encoding = filterConfig.getInitParameter("encoding");
22    String value = filterConfig.getInitParameter("ignore");
23    if (value == null)
24     this.ignore = true;
25    else if (value.equalsIgnoreCase("true"))
26     this.ignore = true;
27    else if (value.equalsIgnoreCase("yes"))
28     this.ignore = true;
29    else
30     this.ignore = false;
31   }
32  
33   public void doFilter(ServletRequest request, ServletResponse response,
34     FilterChain chain) throws IOException, ServletException {
35    // Conditionally select and set the character encoding to be used
36    if (ignore || (request.getCharacterEncoding() == null)) {
37     String encoding = selectEncoding(request);
38     if (encoding != null) {
39      request.setCharacterEncoding(encoding);
40     }
41  
42    }
43    // Pass control on to the next filter
44    chain.doFilter(request, response);
45   }
46  
47   protected String selectEncoding(ServletRequest request) {
48    return (this.encoding);
49   }
50  
51   public void destroy() {
52    this.encoding = null;
53    this.filterConfig = null;
54   }
55  }
56   


web.xml添加此Filter:


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> 1  <filter>
 2   <filter-name>encodeFilter</filter-name>
 3   <filter-class> com.aicent.ccb.filter.SetCharacterEncodingFilter</filter-class>
 4   <init-param>
 5    <param-name>encoding</param-name>
 6    <param-value>GBK</param-value>
 7   </init-param>
 8   <init-param>
 9    <param-name>ignore</param-name>
10    <param-value>true</param-value>
11   </init-param>
12  </filter>
13  <filter-mapping>
14   <filter-name>encodeFilter</filter-name>
15   <url-pattern>/control/*</url-pattern>
16  </filter-mapping> 

你可能感兴趣的:(ofbiz)