Home >> Struts
Using Apache Struts 1.x with Tiles and having i18n capability
demonstration with the help of a sample or example WAR web
application:
This example tries to show in a simple and straight forward
way of presenting an example web application having MVC and Tiles
frameworks for rapid environment staging for a web application
with Internationalization or I18N capability.
Following are the key components/parts of this example:
Action class as SampleAction extends org.apache.struts.action.Action
and overriding execute method as follows:
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("In SampleAction");
return mapping.findForward("success");
}
This action class "SampleAction" is mapped in struts-config.xml file
as follows:
<action path="/main" type="in.i2w.ui.SampleAction"
scope="request" name="SampleForm" >
<forward name="success" path="/welcome.do"/>
<forward name="failure" path="/signup.do"/>
</action>
So this shows that if the URL has the path as /main.do, then SampleAction
will be called, and after printing out "In SampleAction", flow moves to
/welcome.do as the action path, so corresponding action definition in
struts-config.xml file as follows:
<action path="/welcome" type="in.i2w.ui.SampleWelcomeAction"
scope="request" name="SampleWelcomeForm" >
<forward name="success" path="sampleWelcomeSuccess"/>
<forward name="failure" path="sampleWelcomeFailure"/>
</action>
This shows that action with path as "/welcome" will be called with execution
of execute method in SampleWelcomeAction action class.
Action class SampleWelcomeAction execute method as:
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
System.out.println("in SampleWelcomeAction");
HttpSession session = request.getSession(false);
Locale locale = new Locale("hi","IN");
session.setAttribute(ComponentConstants.LOCALE_KEY, locale);
return mapping.findForward("success");
}
This SampleWelcomeAction class execute method will do system out println
"in SampleWelcomeAction" on server console/log depending on the server setting.
Then appropriate Locale is to be set in HttpSession, with a key
as org.apache.struts.taglib.tiles.ComponentConstants.LOCALE_KEY.
and then forwarding control to "success" (as defined in struts-config.xml file).
Corresponding section in struts-config.xml file has the path to
"sampleWelcomeSuccess" as shown above.
<forward name="success" path="sampleWelcomeSuccess"/>
As this path is a String and now having "/" and ".do" at begin
and end of this String value, so it is referring to the Tiles
definition in appropriate Tiles definition file with Locale
having language as "hi" and country as "IN", so the final tiles
definition file looked/searched for by tiles framework is
"tiles-defs_hi_IN.xml" instead of the default one "tiles-defs.xml".
The default tiles definition file "tiles-defs.xml" file is mentioned
in struts-config.xml as :
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>
There is no need to mention the Locale specific Tiles definition files
to be mentioned in struts-config XML file.
And there is no need to define any of the text in Message Resource
properties flat file.
web.xml file should have appropriate Action Servlet configuration
as:
<servlet-name>sample-action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
.....
.....
<servlet-mapping>
<servlet-name>sample-action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Appropriate Tiles definition file "tiles-defs_hi_IN.xml" will be described as :
<tiles-definitions>
<definition name="sampleWelcomeSuccess"
path="/front/welcome.jsp">
<put name="title"
value="Subha Pravata" />
</definition>
</tiles-definitions>
So /front/welcome.jsp file will be referring to the values
put in the tiles definition file like:
title=Subha Pravata.
For you reference, welcome.jsp file would look something like:
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<html>
<head>
<title><tiles:getAsString name="title"/></title>
</head>
<body>
</body>
</html>
So thiw welcome.jsp file show the Title bar as "Subha Pravata",
and in absense of the Locale specific tiles file "tiles-defs_hi_IN.xml",
the default tiles definition file "tiles-defs.xml" will be read, and
the title bar of the welcome.jsp will be
print as "Welcome User", as the default tiles definition is as follows:
<tiles-definitions>
<definition name="sampleWelcomeSuccess" path="/front/welcome.jsp">
<put name="title" value="Welcome User" />
</definition>
</tiles-definitions>
I have not provided complete source as it is, but in bits and pieces
for our interested visitor/reader to create this example and see it
running as desired.
Your feedback and comments will be a great help to the Author for
improvement (if any).
If you like to share your comment/suggestions/feedback relating to this Page,
you can do so by droping us an email at
usingframeworks @ gmail . com
with the subject line mentioning URL for this Page (i.e,
/struts-tiles-i18n.php) or use this
LINK.
As per this website's privacy policy, we never disclose your email id,
though we shall post your comments/suggestions/feedback with
your name (optional) and date on this Page. If you don't want your
comments/suggestions/feedback to be shared in this Page, please
mention so in your email to us. Thank you very much.....
If anything missed out , please let me know at
techienjoy at yahoo . com