无论啥语言都会遇到乱码问题,这里,大体给个乱码的解决方案,就是“一致性原则”。
1.页面方面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2.控制器,业务逻辑方面
HttpServletRequest re = ServletActionContext.getRequest();
re.setCharacterEncoding("UTF-8");
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="GlobalMessage" /> <constant name="struts.locale" value="zh_CN" /> <constant name="struts.i18n.encoding" value="UTF-8"/> <constant name="struts.devMode" value="true" />
3.数据库方面
conn = DriverManager.getConnection(ReadConfig.getReadconfig().getJdbcInfo().getUrl()+ReadConfig.getReadconfig().getJdbcInfo().getAp_005()+"?useUnicode=true&characterEncoding=UTF-8", ReadConfig.getReadconfig().getJdbcInfo().getUserName(),ReadConfig.getReadconfig().getJdbcInfo().getUserPass());
ps.executeQuery("set names utf8");
4.服务器方面,tomcat
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
实在不行,就加filter或者
keyword = new String(quer.getBytes("ISO-8859-1"),"UTF-8");