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中配置:
  <Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8"
  3、Struts2的Locale配置
  在Struts.xml中配置
  <constant name="struts.locale" value="zh_CN" />
  <constant name="struts.i18n.encoding" value="UTF-8" />
  4、采用Spring的过滤器对POST页面编码
  <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>
  org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
  <param-name>encoding</param-name>
  <param-value>UTF-8</param-value>
  </init-param>
  <init-param>
  <param-name>forceEncoding</param-name>
  <param-value>true</param-value>
  </init-param>
  </filter>
  <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/</url-pattern>
  </filter-mapping>
  5、页面统一编码为UTF-8
  在common/meta.jsp中
  <meta http-equiv="Cache-Control" content="no-store"/>
  <!-- HTTP 1.0 -->
  <meta http-equiv="Pragma" content="no-cache"/>
  <!-- Prevents caching at the Proxy Server -->
  <meta http-equiv="Expires" content="0"/>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <meta name="generator" content="Mobile-soft.cn" />
  <meta http-equiv="keywords" content="mobile,payment,telecommunication,internet">
  <meta http-equiv="description" content="mobile-soft">
  同时在各页面中包含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的配置
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url"
  value="jdbc:mysql://localhost/mysql?useUnicode=true&amp;characterEncoding=UTF-8"/>
  <property name="username" value="root"/>
  <property name="password" value=""/>
  <property name="maxActive" value="100"/>
  <property name="maxIdle" value="30"/>
  <property name="maxWait" value="1000"/>
  <property name="defaultAutoCommit" value="true"/>
  <property name="removeAbandoned" value="true"/>
  <property name="removeAbandonedTimeout" value="60"/>
  <property name="validationQuery" value="SELECT 1"/>
  <property name="testOnBorrow" value="true"/>
  </bean>

采用以上步骤基本上搞定了乱码问题,但在使用Struts2的datetimepicker控件时候出现了乱码问题,Struts2的 javascript标签库缺省是采用庞大的dojo库(为何不采用jquery这样清爽的javascript库),因此怀疑是dojo的i18n问题,解决步骤如下:
  1、修改struts.serve.static的缺省配置
  修改struts.mxl,增加如下内容。
  <constant name="struts.serve.static" value="false" />
  在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 <context_path>/struts
  struts.serve.static=true
  注意这里的<context_path>/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" %>
  <html>
  <head>
  <title>tag list</title>
  <%@ include file="/common/meta.jsp" %>
  <s:head/>
  </head>
  <body>
  <s:form name="form1" action="test2">
  <s:datetimepicker name="start_date" language="zh-cn" label="计费开始时间" displayFormat="yyyy-MM-dd" />
  <s:submit/>
  </s:form>
  </body>
  </html>
  注意这里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)
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  Tomcate设置编码(server.xml)
  <Connector 其他省略 port="80" URIEncoding="UTF-8">
  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)
  <filter>
  <filter-name>setcharacterencodingfilter</filter-name>
  <filter-class>com.sorc.SetCharacterEncodingFilter</filter-class>
  <init-param>
  <param-name>encoding</param-name>
  <param-value>utf8</param-value>
  </init-param>
  </filter>
  <filter-mapping>
  <filter-name>setcharacterencodingfilter</filter-name>
  <url-pattern>/</url-pattern>
  </filter-mapping>
  /有了上面的基础下面试完满解决方案/
  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结构和数据时存在乱码.

你可能感兴趣的:(乱码)