(1)每个JSP页面都设置了<%@ page contentType="text/html;charset=UTF-8"%>
(2) web.xml文件中配置了EncodingFilter 使用的spring框架中现成的类. 设置为了UTF-8
(3)Mysql5.5数据库建表时也采用了UTF-8
现象为数据表的图形操作界面中可以插入和显示中文...正常的中文也可以在JSP页面中正常显示...但是.从JSP页面提交的中文
存入数据库中后是乱码....找到了原因是Action类 ,.这一步就出了问题...
在代码中加入红色粗体部分后.乱码问题消失..但这不是有效的解决办法...谁能给点建议???????
public ActionForward addNews(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DynaValidatorForm addNewsForm=(DynaValidatorForm)form;
String title=(String)addNewsForm.get("title");
String cpmtemt=(String)addNewsForm.get("cpmtemt");
byte[] b=cpmtemt.getBytes("ISO-8859-1");
cpmtemt=new String(b,"UTF-8");
String categoryId=(String)addNewsForm.get("categoryId");
Category category=mgr.getCategory(categoryId);
User user=mgr.getUserByName((String)request.getSession().getAttribute("username"));
System.out.println("fdsajfsakfj中欧昂倒萨方的哦暗示飞 "+cpmtemt+" "+title);
try {
News news=new News();
news.setCategory(category);
news.setCpmtemt(cpmtemt);
news.setLastModifyDate(new Date());
news.setPostDate(new Date());
news.setTitle(title);
news.setUser(user);
mgr.saveNews(news);
request.setAttribute("news", news);
} catch (Exception e) {
e.printStackTrace();
return mapping.findForward("errorpage");
}
return mapping.findForward("detailNews");
}
通过参考"关于GET和POST方法的编解码问题"一文找到解决上边问题的根本方法....设置JSP页面表单的提交方法为:POST
代码如下: <html:form action="/newsManagerAction" method="post" focus="categoryName">
去掉上边代码中的红色字体的代码
byte[] b=cpmtemt.getBytes("ISO-8859-1");
cpmtemt=new String(b,"UTF-8"); 中文的保存,显示都正常了....造成这种现象的根本原因是,编码过滤器Filter,只对Post方法的提交编解码有效没有考虑Get方法...可以自己编译编码过滤器来支持Get方法中存在中文传输.
简单的说:spring 框架的编码filter 只适用于post方法.对get的方法无效.
本文介绍了解决JSP页面提交中文至数据库时出现乱码的问题。通过调整编码方式和使用POST方法,确保了中文数据能正确地被存储及显示。

1688

被折叠的 条评论
为什么被折叠?



