今天在解决webwork+spring+mysql 乱码的心得:
首先在mysql创建数据库的时候选择GBK编码;
然后在ACTION中调用DAO时不需要转码,但是在页面显示标签的对象却要转码,例如:
unit.setUnitId(targetUnitValue);
unit.setUnitName(targetValue);
unit.setStateId(unit.getStateId());
unit.setPeopleId(unit.getPeopleId());
boolean tempFlag2 =xmDao.updateUnitInfo(unit); //不需要转码
unit.setUnitName(EncodingUtil.textValueEncoding(targetValue)); //unit.UnitName是要显示在页面标签的,所以要转码才能编码:
EncodingUtil.textValueEncoding:
public static String textValueEncoding(String content) {
if (content == null) {
return "";
}
try {
return new String(content.getBytes("ISO-8859-1"), "GBK");
}
catch(Exception e) {
e.printStackTrace();
}
return null;
}