encoding problems: UTF-8 vs ISO-8859-1 2007-02-05 - By Pierre Thibaudeau
Back I seem to have a problem of consistency between UTF-8 (See http://UTF-8.ora-code.com) and ISO-8859 (See http://ISO-8859.ora-code.com)-1 when it comes to accepting Strings in forms. Here's how it goes:
* My application is intended to be entirely UTF-8 (See http://UTF-8.ora-code.com) encoded: the database, the JSP files, etc. Only exceptions are the localized ApplicationResources_??.properties files, which are encoded as ISO-8859 (See http://ISO-8859.ora-code.com)-1, as per http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html That part works very well.
* I use Tiles, and I include the following line at the beginning of every tile and every layout page: <%@ page contentType="text/html;charset=UTF-8 (See http://UTF-8.ora-code.com)" language="java" pageEncoding="UTF-8 (See http://UTF-8.ora-code.com)"%>
* Also, in the tile that generates the <HEAD> of the HTML files, I include the following: <meta http-equiv="Content-Type" content="text/html; charset=utf-8 (See http://utf-8.ora-code.com)" />
So far, everything works fine and all the localized messages display properly with all the desired accents and diacritic signs.
Everything... except in the case of a text form: <html:form action="/search" acceptCharset="UTF-8 (See http://UTF-8.ora-code.com)"> <html:text property="query"/><br/> <html:submit titleKey="search.search" > <bean:message key="search.executeSearch" /> </html:submit> </html:form>
The ActionForm is defined dynamically in the struts-config.xml file: <form-bean name="NameSearchForm" type=" org.apache.struts.action.DynaActionForm "> <form-property name="query" type="java.lang.String " /> </form-bean>
And the Action: <action path="/search" name="NameSearchForm" scope="request" type="com.mystuff.NameSearchAction" validate="false"> <forward name="results" path=".results" redirect="false" /> <forward name="noresult" path=".noresult" redirect="false" /> </action>
On input of a query string, the NameSearchAction retrieves this string: String queryStr = (String) PropertyUtils.getSimpleProperty(form, "query");
But that's where I get into trouble. Suppose, in the text form, I type the query "C?sar" (the second letter being an e-acute). If on the following "noresult" JSP page, I re-display the content of the query, I then get: "C?(c)sar". (The second and third characters are now respectively an uppercase-A-tilde and a copyright-sign.) That, to me, looks like a conflict between ISO-8859 (See http://ISO-8859.ora-code.com)-1 and UTF-8 (See http://UTF-8.ora-code.com). Where's the catch?
[Using Struts 1.3.5, Java 5, Tomcat 6.0; this experiment is run within Eclipse/MyEclipse 5.1.0.]
|
|