|
今天用Spring进行中文插入时出现乱码问题,通过查资料和自己反复测试终于解决了. 在进行数据保存之前进行gb2312到iso8859-1编码的转换,applicationContext.xml中的数据库连接必须设置为<property name="url"><value>jdbc:mysql://localhost/struts</value></property>,这样插入的才是正常的中文,否则就是乱码。 它们相同的地方是在用jsp进行中文内容填加时,都要进行gb2312到iso8859-1编码的转换:
String name; name=trans(request.getParameter("name"));
String trans(String chi) { String result = null; byte temp []; try { temp=chi.getBytes("iso-8859-1"); result = new String(temp); } catch(java.io.UnsupportedEncodingException e) { System.out.println (e.toString()); } return result; } String trans(Object chi) { return trans(chi.toString()); } |
|
一共有 0 条评论