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 }
<?xml version="1.0"?> <!DOCTYPE stylesheet [ <!ENTITY nbsp " "> ]> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="4.01" doctype-public="-//W3C//DTD HTML 4.01//EN" doctype-system="http://www.w3.org/TR/html4/strict.dtd" encoding="iso-8859-1" indent="yes"/> <!-- This is the main template. It looks at the pageName and dispatch to the appropriate template. The page header and footer are also here. --> <xsl:template match="page"> <html> <head> <title> <xsl:value-of select="//applicationresources/key[@name='test.title']"/> </title> </head> <body> <xsl:variable name="pageName" select="@name"/> <center> <h1> <xsl:value-of select="//applicationresources/key[@name='test.heading0']"/> <xsl:value-of select="$pageName"/> </h1> </center> <xsl:apply-templates select="/page/request/*[name()=$pageName]"/> </body> </html> </xsl:template> <!-- This is the template for the test form. Is display a simple HTML form and iterate through the list to find out the value of the SELECT --> <xsl:template match="TestForm"> <h2> <xsl:value-of select="//applicationresources/key[@name='test.heading1']"/> </h2> <br/> <form name="testForm" method="get" action="result"> <input type="text" name="testString"> <xsl:attribute name="value"> <xsl:value-of select="testString"/> </xsl:attribute> </input> <br/> <select name="outSelect"> <xsl:for-each select="testList/item"> <option> <xsl:value-of select="node()"/> </option> </xsl:for-each> </select> <br/> <xsl:element name="input"> <xsl:attribute name="type">submit</xsl:attribute> <xsl:attribute name="value"> <xsl:value-of select="//applicationresources/key[@name='button.submit']"/> </xsl:attribute> </xsl:element> </form> <hr/> </xsl:template> <!-- This template displays the result page --> <xsl:template match="ResultForm"> <h2> <xsl:value-of select="//applicationresources/key[@name='test.heading2']"/> </h2> <table> <tr> <td> <xsl:value-of select="//applicationresources/key[@name='test.label1']"/> </td> <td> <xsl:apply-templates select="testString"/> </td> </tr> <tr> <td> <xsl:value-of select="//applicationresources/key[@name='test.lavel2']"/> </td> <td> <xsl:apply-templates select="outSelect"/> </td> </tr> </table> <hr/> <a href="test"> <xsl:value-of select="//applicationresources/key[@name='test.link1']"/> </a> </xsl:template> </xsl:stylesheet>
XSLServlet.java
Description: Binary data
ApplicationResources_fr.properties
Description: Binary data
ApplicationResources.properties
Description: Binary data
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <!-- Struts Action Servlet --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>application</param-name> <param-value>ApplicationResources</param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>validate</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Bean to XML transformation Servlet --> <servlet> <servlet-name>XSLServlet</servlet-name> <servlet-class>com.jphint.suiteresponse.struts.XSLServlet</servlet-class> <init-param> <param-name>xsl-file</param-name> <param-value>/WEB-INF/test.xsl</param-value> </init-param> <init-param> <param-name>application</param-name> <param-value>ApplicationResources</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Servlet Mapping --> <servlet-mapping> <servlet-name>XSLServlet</servlet-name> <url-pattern>/XSLServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>/do/*</url-pattern> </servlet-mapping> </web-app>
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>