How to handle UTF-8 characters in struts ApplicationResources.properties

N

ningjun.wang

I used WSAD5 and struts to develop a Japaness site using UTF-8. I
defined error message in strut's ApplicationResources.properties file.
If the error message contains Japaness characters (UTF-8 encoding), it
will not be displayed correctly in the browser.

For example ApplicationResources.properties contains the following:

error.login.invalidCredential=å‰å·è‹±ä¸€éƒŽ: Your userName/password
is invalid. Please try again.

I raise this error in my action class as follow:

errors.add("login", new ActionError("error.login.invalidCredential"));

In the JSP I displayed the error using
<html:errors />

When I run the application, the JSP display the following message:

å‰å·Â英一郎: Your userName/password is invalid.
Please try again.


Note that the English characters are displayed correctly but not the
Japaness characters.

What is wrong?

Ningjun
 
J

John C. Bollinger

I used WSAD5 and struts to develop a Japaness site using UTF-8. I
defined error message in strut's ApplicationResources.properties file.
If the error message contains Japaness characters (UTF-8 encoding), it
will not be displayed correctly in the browser.

For example ApplicationResources.properties contains the following:

error.login.invalidCredential=å‰å·è‹±ä¸€éƒŽ: Your userName/password
is invalid. Please try again.

I raise this error in my action class as follow:

errors.add("login", new ActionError("error.login.invalidCredential"));

In the JSP I displayed the error using
<html:errors />

When I run the application, the JSP display the following message:

å‰å·Â英一郎: Your userName/password is invalid.
Please try again.


Note that the English characters are displayed correctly but not the
Japaness characters.

What is wrong?

It certainly looks like a character encoding mismatch. The default
character encoding for HTTP is ISO-8859-1 (and this is also explicitly
specified by JSP as the default encoding for responses). I observe that
all the incorrect characters you provided do appear to be among those
provided by ISO-8859-1. To indicate the correct charset in straight JSP
you would specifically set the character encoding for the response (and
the default encoding for the JSP text) by means of the contentType
attribute of a <@page> directive. For example:

<@page contentType="text/html; charset=UTF-8">

In a servlet you would invoke the response object's setContentType()
method. Struts may have a convenient way to handle this for you; I
don't know Struts well enough to tell you one way or another.


John Bollinger
(e-mail address removed)
 
O

Oscar kind

I used WSAD5 and struts to develop a Japaness site using UTF-8. I
defined error message in strut's ApplicationResources.properties file.
If the error message contains Japaness characters (UTF-8 encoding), it
will not be displayed correctly in the browser.

For example ApplicationResources.properties contains the following:

error.login.invalidCredential=?????: Your userName/password
is invalid. Please try again.

Please note that a .properties file use the ISO 8859-1 character encoding,
so using UTF-8 characters in those files is not supported. See the
javadocs for java.util.RecourceBundle, java.util.PropertyResourceBundle
and java.util.Properties; especially this part:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html#encoding

BTW, struts uses java.util.Properies directly.
 
N

ningjun.wang

I already set JSP contentType to UTF-8 using
res.setContentType("text/html; charset=UTF-8");
in the servlet as well as
<@page contentType="text/html; charset=UTF-8">
in the JSP. It does not work.

By the way, I have many Japaness characters displayed on the same JSP
correctly. Only those error messages presented by <html:errors /> do
not display correctly. Therefore I believe the problem is how struts
read the message property file. What encoding does struts use to read
message property files?

Ningjun
 
Y

yzzzzz

I used WSAD5 and struts to develop a Japaness site using UTF-8. I
defined error message in strut's ApplicationResources.properties file.
If the error message contains Japaness characters (UTF-8 encoding), it
will not be displayed correctly in the browser.

For example ApplicationResources.properties contains the following:

error.login.invalidCredential=å‰å·è‹±ä¸€éƒŽ: Your userName/password
is invalid. Please try again.

I raise this error in my action class as follow:

errors.add("login", new ActionError("error.login.invalidCredential"));

In the JSP I displayed the error using
<html:errors />

When I run the application, the JSP display the following message:

å‰å·Â英一郎: Your userName/password is invalid.
Please try again.


Note that the English characters are displayed correctly but not the
Japaness characters.

What is wrong?

Ningjun

Have you tried using
error.login.invalidCredential=\u5409\u5DDD\u82F1\u4E00\u4E00
(Note the codepoints may be wrong, I didn't check)
 
N

ningjun.wang

Have you tried using
error.login.invalidCredential=\u5409\u5DDD\u82F1\u4E00\u4E00

Yes, it works. I hope I can put real Japaness characters such as å‰å·
there. But I can live with using \u.

Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top