3

I've a Struts2 application with some encoding problems. My JSP page displays 2 forms; when I send the first one (a simple, regular form, which reloads the full page), non-standard characters such as á or ñ are sent and shown correctly. However, when I do the same with the second form (which is sent via AJAX), the data gets corrupted (Ãí, Ã! and so on).

main.jsp:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    ...
    <s:head theme="ajax"/>
</head>
<body>
    <!-- This form is sent via regular HTTP request -->
    <s:form theme="simple" enctype="multipart/form-data">
        <s:textfield key="field1" name="var1"/>
        <s:submit key="send" action="SAVE_ACTION"/>
    </s:form>
    <div id="ajaxContainer">
         <jsp:include file="ajax-part.jsp"/>
    </div>
</body>
</html>

ajax-part.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!-- This form is sent via AJAX -->
<s:form enctype="multipart/form-data" theme="ajax" action="AJAX_ACTION" acceptcharset="UTF-8">
    <s:textfield key="field2" name="var2"/>
    <s:submit key="send" targets="ajaxContainer"/>
</s:form>

As you can see, I've set UTF-8 encoding everywhere. I've also checked that the files are actually UTF-8 encoded. And DOJO (which is used for the AJAX requests) is configured in UTF-8 too, because <s:head theme="ajax"/> becomes:

<script language="JavaScript" type="text/javascript">
    // Dojo configuration
    djConfig = {
        baseRelativePath: "/myApp/struts/dojo",
        isDebug: false,
        bindEncoding: "UTF-8",
        debugAtAllCosts: false
    };
</script>

I'm running the application on a Tomcat 6 server; adding the -Dfile.encoding=UTF-8 parameter makes no difference (appart from the log files being recorded in UTF-8 instead of ISO-8859-1).

It happens both in Google Chrome and IE8, so I guess the browser has nothing to do with the problem, it must be something in the webapp.

What am I missing? What can I change to process correctly my AJAX posts?

AJPerez
  • 3,435
  • 10
  • 61
  • 91
  • What happens if you remove acceptcharset="UTF-8" from ? And also try removing bindEncoding: "UTF-8" from the ajax too? – Javi Dec 23 '10 at 10:44
  • Without acceptcharset="UTF-8" it seems to work just the same way. As for the bindEncoding parameter for the DOJO configuration, I don't know how to remove or change it.. – AJPerez Dec 23 '10 at 11:54
  • Struts2 dojo has been deprecated (I forget when but by 2.2.1 it definitely has). I don't think it is server side because you can post your data correctly with a normal form. The same interceptor stack is used my guess is how dojo is preparing the url. Perhaps manage the ajax manually (jQuery and similar javascript libraries). – Quaternion Dec 23 '10 at 19:00
  • 1
    Have you inspected the ajax request with firebug? – Quaternion Dec 24 '10 at 00:01
  • Thanks for the Firebug tip, Quaternion. Finally I was able to solve the problem :) Although I'd like to have a better solution... – AJPerez Dec 24 '10 at 09:21

1 Answers1

8

Solved!

After inspecting AJAX requests with Firebug (thanks for the tip, Quaternion) I found out the problem: Dojo was sending correctly the data in GET requests, with the parameters URL-encoded. First I thought that Struts2 was doing the URL-decoding with the wrong charset, thus corrupting the data; but as Steven Benitez pointed out, it was actually the underlying Servlet API.

The problem was finally solved by setting URIEncoding="UTF-8" to the <Connector> on Tomcat's server.xml, as explained in Apache Tomcat FAQs.

In case this approach doesn't work for somebody else, it could also be manually solved by adding the following conversion where the AJAX data is received:

new String(myData.getBytes("ISO-8859-1"), "UTF-8");
AJPerez
  • 3,435
  • 10
  • 61
  • 91
  • 1
    Struts2 doesn't URL decode. The underlying Servlet API does. http://stackoverflow.com/questions/469874/how-do-i-correctly-decode-unicode-parameters-passed-to-a-servlet – Steven Benitez Dec 24 '10 at 16:54
  • Your link led me to this page http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q2 - which I've immediately bookmarked. It seems that I've finally found how to read the GET data in the correct encoding :). I'll have to wait 'til next monday to test it, but it should work. Thanks a lot!! – AJPerez Dec 24 '10 at 17:28