Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

struts2 How to set UTF-8 char encoding

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

My Ajax function looks like

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.

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

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

<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

Thanks in advance.

like image 619
alagusathish Avatar asked Oct 18 '25 13:10

alagusathish


1 Answers

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

<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

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

request.setCharacterEncoding("UTF-8");
like image 62
Ravi K Thapliyal Avatar answered Oct 20 '25 02:10

Ravi K Thapliyal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!