碰到中文问题主要有几类:
1)从confluence的mysql数据库导出的UTF-8数据
2)http get/post请求中汉字
3)ajax xmlhttprequest传递的汉字
4)mysql环境设置
5)spring /velocity /jboss环境的中文配置
成功的文件配置关键内容如下:
1.1spring2应用Web.xml配置
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
GB2312
forceEncoding
true
1.2Ibatissqlmap-config.xml
1.3ibatis jdbc配置:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/scmproj?useUnicode=true&characterEncoding=UTF-8
1.4spring mvc:
WEB-INF/velocity/
GB2312
GB2312
text/html;charset=GB2312
false
1
true
templates.vm
org.apache.velocity.runtime.log.SimpleLog4JLogSystem
com.ali
true
true
false
true
.vm
true
true
true
dateTool
由于页面上文字是汉字,但数据库为utf-8。VelocityViewResolver的contentType必须为UTF-8 ,否则部分复杂汉字无法正常显示。如璟。
1.5Mysql5.0创建数据库:
create database scmproj DEFAULT CHARACTER SET utf8;
1.6mysql的配置文件my.ini:
[mysql]
default-character-set=gb2312
[mysqld]
default-character-set=utf8
1.8velocity表单提交前编码
Function GetByteCode(ch)
code = Asc(ch)' -20001
GetByteCode = Hex(code) ' "B1DF"
End Function
String.prototype.urlEncode = function() {
var r = "";
for (var i = 0; i < this.length; i++) {
var code = "" + GetByteCode(this.charAt(i));
if (code.length > 2) {
var b1 = code.substr(0, code.length - 2);
var b2 = code.substr(code.length - 2);
if (i == 0) {
//alert("" + code + ", " + b1 + ", " + code.length + ", " + b2);
}
r += "%" + b1 + "%" + b2;
} else {
r += "%" + code;
}
}
return r;
}
Function checkform()
{
var ōldvalue= document.formtips.content.value;
if(oldvalue=="")
{
alert("请设置值");
return false;
}
var urlEncodeResult=oldvalue.urlEncode();
document.formtips.content.value=urlEncodeResult;
}
1.9接收form表单的服务器端代码
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
try {
SCMTips tips = (SCMTips) command;
if (logger.isInfoEnabled()) {
logger.info("before decode content:" + tips.getContent());
logger.info("requestEncode="+ request.getCharacterEncoding());
}
String decoderesult = java.net.URLDecoder.decode(tips.getContent(),
"gb2312");
if (logger.isInfoEnabled()) {
logger.info("decode result:" + decoderesult);
}
1.10AJAX发送端
functionmakeHttpReq()
{
var new_httpreq = null;
if (window.XMLHttpRequest) {
new_httpreq = new XMLHttpRequest();
if (new_httpreq.overrideMimeType){
new_httpreq.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) {
try{
new_httpreq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
new_httpreq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
}
}
}
returnnew_httpreq;
}
if (httpReq) {
httpReq.onreadystatechange = function(){
if(httpReq.readyState == 4)
{
//var console=document.getElementById('console');
//alert(httpReq.responseText);
//console.innerHtml = httpReq.responseText;
}
};
httpReq.open("GET", url, true);
httpReq.setRequestHeader("If-Modified-Since","0");
httpReq.send(null);
}
从上面可以看到httqreq没有指定编码格式。默认就是ISO-8859-1。
2.11 接收ajax处理的服务器端代码
public ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (request.getMethod().equals("GET")) {
String encodeProj = request.getParameter("projectname");
if (logger.isInfoEnabled()) {
logger.info("before decode:" + encodeProj);
}
//客户端经过编码后的汉字
String projectname= new String( encodeProj.getBytes( "8859_1" ) , "gb2312" );
if (logger.isInfoEnabled()) {
logger.info("after decode:" + projectname);
}2.12 JBOSS4.2配置
D:\jboss-4.2.2.GA\server\default\deploy\jboss-web.deployer \Server.xml 里面
-->
maxThreads="250" maxHttpHeaderSize="8192"
emptySessionPath="true" protocol="HTTP/1.1"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
没有修改URIEncoding