You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Craig Laverty <dl...@bigpond.net.au> on 2002/04/12 18:23:46 UTC
Model 2x and i18n first try.
Hi All,
I have taken the example servlet discussed in the article
http://www.javaworld.com/javaworld/jw-02-2002/jw-0201-strutsxslt.html and
added i18n support. Having a very short history with java, first I tried to
find and use other people's solutions to implement xslt with struts, but
what I liked about the Model2x implementation was how self contained it was,
giving you the ability to move to newer versions of Struts with little or no
hassle. Unfortunately I haven't found any more examples, and I was in
desperate need of i18n support, so finally I decided to try and write an
i18n solution myself using the Model2x servlet as the base.
Link to previous discussion on Model 2x
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23094.html
What I am hoping to come of this posting is for some good constructive
criticism. Being somewhat of a rookie I know I have a lot to learn about
web application development and about struts. Maybe through replies to this
post I can develop a more solid Model2x - i18n solution and gain some
practical experience.
Why would I want to use struts - xslt instead of struts - jsp?
We have a prototype webapplication that uses xslt for display, and now we
would like to add i18n to it plus a lot of the other key features that
struts brings to the table. We have the xslt experience to make the view
side of things work, and I am hoping to save time by making use of our
existing style sheets.
I load and handle the message resources in the XSLServlet. I decided
against extending existing classes used by the action servlet for message
resources because I thought it would be hard to maintain.
Because I have added all the messages that match the user's locale to the
xml tree, I decided to store the messages as a hashmap and put them in the
servlet context. This allows me to load it once and use it often. There is
one default messages hashmap and one messages hashmap for each (resource
file - user locale) match.
I have attached the updated XSLServlet.java and other modified files from
the originals. The original files can be found by clicking on the above
javaworld link.
pseudo code for proposed i18n solution for Model 2x
init () {
load the messages hashmap into the servlet context using the default
locale as key
add to the locales hashmap using the default locale as the key and the
default locale as the value
... timer returned 20 ms
}
On action forward to the XSLServlet :
Determine if the user's locale has already been loaded (It will have been
loaded during the initialization of the servlet if the user's locale matches
the default locale, or the resource will already exist if a previous user
had the same locale setting in their browser)
if (yes) {
call getResourceMessagesHashMap()
call serializeMessagesHashMap()
... timer returned 0 ms
}else {
call loadNewMessagesHashMap()
call serializeMessagesHashMap()
... timer returned 0 ms if it used the default locale already in the
servlet context and returned 20 ms if it created a new messages hashmap for
the user's locale
}
getResourceMessagesHashMap() {
Determine if the users locale exists in the servlet context
if (yes) {
return the messages hashmap from the servlet context using the
user's locale as the key
} else {
return the messages hashmap using the default locale as key
}
}
setResourceMessagesHashMap() {
Determine if the user's locale matched an applicationResources file
if (yes) {
load the messages hashmap into the servlet context using the user's
locale as key
} else {
don't place message hashmap in the servlet context, but
place the user's locale as the key and the default locale as the
value in the locales hashmap - this way we know the current user's locale
has been handled and it will direct the next request to the default locale
message hashmap stored in the servlet context.
}
}
loadNewMessagesHashMap() {
Determine if there is a resource file that matches the user's locale
if (yes) {
load the file, and populate the messages hashmap
put the messages hashmap into the servlet context using the user's
locale as the key
} else {
use the default locale hashmap already stored in the servlet context
during the init process
}
return the messages hashmap
}
serializeMessagesHashMap {
create a dom element from the messages hashmap and insert it into the
dom document
}