Tuesday, April 21, 2009

How to use unicode resources with <bean:message> tag in Struts 1

Unicode becomes popular in programming. However, in some cases, you cannot use direct Unicode encoding text. The example below is one of those cases.

In the welcome.jsp page, I want to display Korean texts which come from:
- Static text in the source code of welcome.jsp (page encoding = utf-8)
- Message load from properties file using <bean:mesasge> tag
- String bean value get from <bean:write> tag (Unicode string)

I have tried many ways to make all the three strings display correctly but always only two values are displayed correctly. The problem is that <bean:message> writes the value using system default character set (usually ISO-8859-1) Cause I don't understand clearly about i188n multilingual and locale so I cannot find the solution using locale. At last, I found the approach converting from original Unicode encoding to ASCII presentation of Unicode a very convenient way. The conversion can be done by native2ascii tool (come with JDK)

Welcome.jsp

<% @ Taglib uri = "/ tags / struts-bean" prefix = "bean"%>
<% @ Taglib uri = "/ tags / struts-html" prefix = "html"%>
<% @ Taglib uri = "/ tags / struts-logic" prefix = "logic"%>
<% @ Page contentType = "text / html; charset = utf-8" pageEncoding = "utf-8"%>
<html>
<head>
<title> Welcome </ title>
</ head>
<body>
Static <p>: 표제어
<p> Load from resource:
<bean:message key="main.title">
<p> get from server:
<bean:write name="name" scope="session">
</ body>
</ html>

korean_utf8.properties

main.title=표제어

korean_ascii.properties

main.title=\ud45c\uc81c\uc5b4

1 comment:

John Tee said...

Thanks. This is very helpful.