09年计算机二级辅导:计算机二级JAVA技巧(乱码解决方案)

1、JVM中缺省字符集
  或者在环境变量中配置JAVA_OPTS=%JAVA_OPT%;-Dfile.encoding=GBK -Duser.language=zh_CN
  或者在Tomcat的启动脚本中配置JAVA_OPTS=%JAVA_OPT%;-Dfile.encoding=GBK -Duser.language=zh_CN
  2、Tomcat的URIEncoding ,处理GET方式的乱码问题
  在server.xml中配置:
    3、Struts2的Locale配置
  在Struts.xml中配置
  
  
  4、采用Spring的过滤器对POST页面编码
  
  encodingFilter
  
  org.springframework.web.filter.CharacterEncodingFilter

  
  encoding
  UTF-8
  

  
  forceEncoding
  true
  

  

  
  encodingFilter
  /
  

  5、页面统一编码为UTF-8
  在common/meta.jsp中
  
  
  
  
  
  
  
  
  
  同时在各页面中包含meta.jsp页面及设定pageEncoding:
  <%@ include file="/common/meta.jsp" %>
  <%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html;charset=GBK" %>
  6、数据库编码
  数据库建库时候字符集编码采用UTF-8
  在applicationContext-resources.xml中,mysql的配置
    destroy-method="close">
  
    value="jdbc:mysql://localhost/mysql?useUnicode=true&characterEncoding=UTF-8"/>
  
  
  
  
  
  
  
  
  
  
  


采用以上步骤基本上搞定了乱码问题,但在使用Struts2的datetimepicker控件时候出现了乱码问题,Struts2的 javascript标签库缺省是采用庞大的dojo库(为何不采用jquery这样清爽的javascript库),因此怀疑是dojo的i18n问题,解决步骤如下:
  1、修改struts.serve.static的缺省配置
  修改struts.mxl,增加如下内容。
  
  在struts2-core-2.0.11.jar/org/apache/struts2/default.properties中, struts.serve.static缺省值为true,也即从struts2-core-2.0.11.jar查找Struts2的静态文件,这参数的命名太晦涩。
  ### Used by FilterDispatcher
  ### If true then Struts serves static content from inside its jar.
  ### If false then the static content must be available at /struts
  struts.serve.static=true
  注意这里的/struts实际指的就是jsp页面所在目录。
  2、覆盖缺省的静态文件
  在resource目录(与WEB-INF同级或WEB-INF下)创建struts目录,并:
  解压struts2-core-2.0.11.jar:/org/apache/struts2/static/ to /struts/
  解压struts2-core-2.0.11.jar:/template/simple/dojoRequire.js to /struts/simple/
  解压struts2-core-2.0.11.jar:/template/xhtml/styles.css to /struts/xhtml/
  解压struts2-core-2.0.11.jar:/template/xhtml/validation.js to /struts/xhtml/
  解压struts2-core-2.0.11.jar:/template/css_xhtml/styles.css to /struts/css_xhtml/
  解压struts2-core-2.0.11.jar:/template/css_xhtml/validation.js to /struts/css_xhtml/
  解压struts2-core-2.0.11.jar:/template/ajax/dojoRequire.js to /struts/ajax/
  解压struts2-core-2.0.11.jar:/template/ajax/validation.js to /struts/ajax/
  注意:以上所说的解压并不是把整个包都解压,只是把需要的内容勇winrar直接拽出来,对于struts2-core-2.0.11.jar包还是保持原状,不要做任何改动,以方便后期的升级。
  3、修改js文件的编码方式
  用记事本打开struts/dojo/src/i18n/calendar/nls/zh/gregorian.js并以ASSI格式另存为gregorian.js
  用记事本打开struts/dojo/src/i18n/calendar/nls/zh/gregorianExtras.js并以ASSI格式另存为gregorianExtras.js
  用记事本打开struts/dojo/src/i18n/calendar/nls/zh-cn/gregorian.js并以ASSI格式另存为gregorian.js
  以上几个文件,原来的格式是UTF-8的
  4、页面实例
  <%@ taglib prefix="s" uri="/struts-tags" %>
  <%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
  
  
  tag list
  <%@ include file="/common/meta.jsp" %>
  
  
  
  
  
  
  

  
  
  注意这里language为zh-cn(也可以为zh),而不是zh_CN,这与java中不同。dojo官方在Internationalization (i18n)文档中强调:
  Locale
  dojo.locale
  The locale is a short string, defined by the host environment, which conforms to RFC 3066 used in the HTML specification. It consists of short identifiers, typically two characters long which are case-insensitive. Note that Dojo uses dash separators, not underscores like Java (e.g. "en-us", not "en_US"). Typically country codes are used in the optional second identifier, and additional variants may be specified. For example, Japanese is "ja"; Japanese in Japan is "ja-jp". Notice that the lower case is intentional -- while Dojo will often convert all locales to lowercase to normalize them, it is the lowercase that must be used when defining your resources.
  The locale in the browser is typically set during install and is not easily configurable. Note that this is not the same locale in the preferences dialog which can be used to accompany HTTP requests; there is unfortunately no way to access that locale from the client without a server round-trip.
  The locale Dojo uses on a page may be overridden by setting djConfig.locale. This may be done to accomodate applications with a known user profile or server pages which do manual assembly and assume a certain locale. You may also set djConfig.extraLocale to load localizations in addition to your own, in case you want to specify a particular translation or have multiple languages appear on your page.
  java、jsp中设置编码
  开发工具会有好多地方设置编码
  下面两种设置编码格式方法适用于jsp页面(.jsp)
  <%@ page language="java" import="java.util." pageEncoding="UTF-8"%>
  <%@ page contentType="text/html; charset=UTF-8" %>
  下面方式适合于jsp、servlet、action中(.java)
  request.setCharacterEncoding("UTF-8");
  response.setCharacterEncoding("UTF-8");

下面适合html页面(.htm;.html)
  
  Tomcate设置编码(server.xml)
  
  mysql设置编码命令
  SET character_set_client = utf8;
  SET character_set_connection = utf8;
  SET character_set_database = utf8;
  SET character_set_results = utf8;/这里要注意很有用/
  SET character_set_server = utf8;
  SET collation_connection = utf8_bin;
  SET collation_database = utf8_bin;
  SET collation_server = utf8_bin;
  my.ini中配置默认编码
  default-character-set=utf8
  连接数据库设置编码
  jdbc:mysql://192.168.0.5:3306/test?characterEncoding=utf8
  /java与mysq编码对应/
  java中的常用编码UTF-8;GBK;GB2312;ISO-8859-1;
  对应mysql数据库中的编码utf8;gbk;gb2312;latin1
  /过滤器使用/
  //过滤器设置编码过滤(SetCharacterEncodingFilter.java)
  package com.sorc;
  import java.io.;
  import javax.servlet.;
  import javax.servlet.http.;
  public class SetCharacterEncodingFilter extends HttpServlet implements Filter{
  private FilterConfig filterConfig;
  private String encoding=null;
  //Handle the passed-in FilterConfig
  public void init(FilterConfig filterConfig){
  this.filterConfig=filterConfig;
  encoding=filterConfig.getInitParameter("encoding");
  }
  //Process the request/response pair
  public void doFilter(ServletRequest request,ServletResponse response,FilterChain filterChain){
  try{
  request.setCharacterEncoding(encoding);
  filterChain.doFilter(request,response);
  } catch(ServletException sx){
  filterConfig.getServletContext().log(sx.getMessage());
  } catch(IOException iox){
  filterConfig.getServletContext().log(iox.getMessage());
  }
  }
  //Clean up resources
  public void destroy(){
  }
  }
  //web.xml配置过滤器方法(web.xmd)
  
  setcharacterencodingfilter
  com.sorc.SetCharacterEncodingFilter
  
  encoding
  utf8
  

  

  
  setcharacterencodingfilter
  /
  

  /有了上面的基础下面试完满解决方案/
  mysql 中查看数据库环境变量命令:show variables ;
  找出环境变量中匹配的字符,例如查看字符编码环境:show variables like 'character%';
  设置数据库字符编码命令:set names gbk 或者 set names utf8
  此命令为零时的, 如果关闭客户端命令窗口,则设置失效, 重新开启时需重新设置。
  1.使用GBK编码的解决方案
  这个最简单 遇到设置编码的地方就是用GBK数据库gbk 然后在使用个过滤器过滤编码为gbk一切搞定。
  效果为添加数据无乱码 读出无乱码 数据库管理工具无乱码 到处sql结构和数据无乱码
  2.使用UTF-8编码解决方案
  所有编码都设置为UTF-8
  数据库编码utf8
  设置过滤器编码utf8
  数据库连接?characterEncoding=utf8
  然后在数据库管理工具或mysql命令行 运行 SET character_set_results = gbk;
  效果为添加数据无乱码 读出无乱码 数据库管理工具无乱码 到处sql结构和数据时存在乱码
  3.页面使用UTF8 数据库使用latin1的解决方案
  jap java tomcat 设置为UTF-8
  过滤器 utf8
  数据库连接?characterEncoding=latin1
  数据库其他latin1
  然后在数据库管理工具或mysql命令行 运行 SET character_set_results = gbk;
  效果为添加数据无乱码 读出无乱码 数据库管理工具无乱码 到处sql结构和数据时存在乱码.

你可能感兴趣的:(2012年6月,乱码)