struts2如何设置UTF-8 char编码

时间:2021-05-27 20:12:22

I have a jsp file where i am collection form values and send it to struts 2 action class through jquery Ajax.

我有一个jsp文件,其中我是集合表单值,并通过jquery Ajax将其发送到struts 2动作类。

My Ajax function looks like

我的Ajax功能看起来像

var DataValues = $("#Form1").serialize();
    alert(DataValues);
    alert(decodeURI(DataValues));
    $.ajax({url: urlPass,
        dataType:datatypepass,
        method:methodpass,
        data:DataValues,
        success: function(data,stat,Xhr){calbackPass(data,stat,Xhr);},
        error:function(xhr, status, error){alert("Error : "+xhr.responseText+" status : "+xhr.status);}
    });

when i decodeurl and alert it the text i correctly encoded and decoded.

当我解码并提醒我正确编码和解码的文本。

when i send it to struts2 through ajax it makes problem.

当我通过ajax将它发送到struts2时会产生问题。

i have checked the values in Interceptor it shows the value ???????

我已经检查了Interceptor中的值,它显示了值???????

Interceptor

拦截器

  public class LoginInterceptor extends AbstractInterceptor implements StrutsStatics
    {
    @Override
        public String intercept(ActionInvocation arg0) throws Exception 
        {
            HttpServletRequest rs=ServletActionContext.getRequest();
            System.out.println(rs.getCharacterEncoding());
            Map session=ActionContext.getContext().getSession();
            Map<String, Object> parameters=ActionContext.getContext().getParameters();
            for(Map.Entry<String, Object> ll:parameters.entrySet())
            {
                String a[]=(String[])ll.getValue();
                for(String b:a)
                {
                    System.out.println(ll.getKey()+" : "+b);
                }   
  }}}}

in my jsp file i have set content type as UTF-8 and in ajax also i have checked with content type but its not working.In tomcat server.xml also i have set content-type as UTF-8

在我的jsp文件中,我已将内容类型设置为UTF-8,而在ajax中我也检查了内容类型,但它无法正常工作。在tomcat server.xml中我也将内容类型设置为UTF-8

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>

any other setting has to be done for this UTF-8

必须为此UTF-8进行任何其他设置

Thanks in advance.

提前致谢。

1 个解决方案

#1


4  

Add a character encoding filter to your web.xml that intercepts all requests before Struts do.

在web.xml中添加一个字符编码过滤器,在Struts执行之前拦截所有请求。

<filter>
  <filter-name>CharacterEncodingFilter</filter-name>
  <filter-class>
    org.apache.catalina.filters.SetCharacterEncodingFilter
  </filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
  </init-param>
  <init-param>
    <param-name>ignore</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>

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

org.apache.catalina.filters.SetCharacterEncodingFilter is available out-of-the-box Tomcat 7.0.20 onwards. Otherwise, just implement your own Filter that sets the character encoding using

org.apache.catalina.filters.SetCharacterEncodingFilter可以直接使用Tomcat 7.0.20开箱即用。否则,只需实现自己的Filter,即使用设置字符编码

Edit: Added / to properly close filter and filter-mapping tags

编辑:添加/以正确关闭过滤器和过滤器映射标签

request.setCharacterEncoding("UTF-8");

#1


4  

Add a character encoding filter to your web.xml that intercepts all requests before Struts do.

在web.xml中添加一个字符编码过滤器,在Struts执行之前拦截所有请求。

<filter>
  <filter-name>CharacterEncodingFilter</filter-name>
  <filter-class>
    org.apache.catalina.filters.SetCharacterEncodingFilter
  </filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
  </init-param>
  <init-param>
    <param-name>ignore</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>

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

org.apache.catalina.filters.SetCharacterEncodingFilter is available out-of-the-box Tomcat 7.0.20 onwards. Otherwise, just implement your own Filter that sets the character encoding using

org.apache.catalina.filters.SetCharacterEncodingFilter可以直接使用Tomcat 7.0.20开箱即用。否则,只需实现自己的Filter,即使用设置字符编码

Edit: Added / to properly close filter and filter-mapping tags

编辑:添加/以正确关闭过滤器和过滤器映射标签

request.setCharacterEncoding("UTF-8");