Struts 项目中 出现乱码

来源:互联网 发布:南昌关键字优化公司 编辑:程序博客网 时间:2024/05/06 10:35
搭建了一个ssm工程,表单传递到Action中后中文都是乱码。
解决办法:
中文乱码,首先要区分是页面乱码、action乱码,还是数据库乱码。大致的原理是java使用unicode编码-->window使用gbk(gb2312的扩展集)--mysql默认使用utf-8(unicode的一种

编码方法),这样转来转去就乱码了^_^。解决方法如下:
   1. 在struts2里面,最好将所有字符都设成utf-8。
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page pageEncoding="UTF-8" %>
   1.1 在jsp页面设定字符编码。这边有必有说明的是如果是jsp+java bean+servlet的方案,中文乱码很好解决,统一设成gb2312就可以了。
    1.2 使用struts框架字符集不能设成gb2312,要改成utf-8。
   2. 在struts.properties 添加:
struts.devMode=false
struts.enable.DynamicMethodInvocation=true
struts.i18n.reload=true
struts.ui.theme=simple

struts.locale=zh_CN
struts.i18n.encoding=UTF-8

struts.serve.static.browserCache=false
struts.url.includeParams=none
 

其中locale、encoding就是字符集的设定了。

   3. 在web.xml加个filter

  <!-- zh-cn encoding -->
    <filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ActionContextCleanUp
        </filter-class>
    </filter> 
    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
纠结了两三个小时,试过N多中方法,最终还是通过方法2解决的!

上面是众网友的方法,实践证明,在我的项目中都没有用啊。
最后的解决办法是,将jsp文件中的Form 的method 改为 post。此刻,我只想呵呵……
0 0
原创粉丝点击