日期:2014-05-17  浏览次数:20701 次

struts2+freemarker中文参数乱码问题
很简单的一个登录页面,获取用户名之后直接返回用户名,然后发现中文用户名返回之后都是乱码

这是什么原因?  加了一个过滤器,在过滤器里用request获取参数值已经是乱码了。。。


网上找了各种方法都是不行,过滤器修改request的编码,改配置文件,都尝试过了。

贴一下现在的配置,

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


freemarker.properties
locale=zh_CN
default_encoding=utf-8
number_format=#
date_format=yyyy-MM-dd
time_format=HH:mm:Ss
datetime_format=yyyy-MM-dd HH:mm:Ss


web.xml


<filter>
<filter-name>EncodingFilter</filter-name>
    <filter-class>Filter.EncodingFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>  
    
  
 <!-- 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>
 
  
<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>


  
    <servlet>
        <servlet-name>JspSupportServlet</servlet-name>
        <servlet-class>
            org.apache.struts2.views.JspSupportServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>



tomcat的配置


    <Connector connectionTimeout="20000" port="8888" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="GBK"/>

------最佳解决方案--------------------
引用:
引用:

引用:

引用:

引用:

自己写的过滤器就是只对request设置了。
Java code


public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) t……