Home >> Struts
Using Apache Struts 2.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.
Note: this example is for learning purpose only and neither to be used
as some proven idea nor treat it as a recommendation to use from this site.
Reader should take appropriate time to evaluate all aspects of
application development criterias before making any final decision.
Following are the key components/parts of this example:
Software environment used for this example :
1. JDK 5.0
2. Tomcat 5.5.x
3. Eclipse Genymede
4. Apache Struts 2.1.6 (example Web application should have
following JAR files in WEB-INF\lib folder)
(Note: This example is not tested with any
other version of Apache Struts )
I have to write following files for this example:
1. MyExample\WEB-INF\classes\struts.xml
2. MyExample\WEB-INF\tiles.xml
3. MyExample\WEB-INF\tiles_hi_IN.xml (Tiles file for the Hindi language
country code as IN)
4. MyExample\WEB-INF\web.xml
5. MyExample\WEB-INF\classes\sample\MyExample.java
6. MyExample\good.jsp
7. MyExample\bad.jsp
8. MyExample\visitor.jsp
9. MyExample\index.html
Please refer earlier example for details related to all the files
required for setting up Struts2 with Tiles (Basic example)
In order to make this example Struts2 web application work with
I18N capability for Tiles, mostly we have to change the Action class
to make it SessionAware or retrieve HttpSession from the HttpServletRequest
object, and then set the LOCALE attribute as the Locale with Language as
"hi" and Country code as "IN".
So the changed Action class is as follows:
MyExample.java
package sample;
/**
* This code is provided "AS IS" without any guaranty
* Use of this code only for educational and learning
* purpose only.
* Author: Amit
* Contact : usingframeworks@gmail.com
* Date: 16-Feb-2009
*/
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Locale;
import java.util.Map;
/**
* This is the Action POJO class
*/
public class MyExample implements SessionAware {
HttpSession session;
public void setSession(Map map) {
System.out.println(map);
this.session = (HttpSession)map.get(0);
}
public String execute() throws Exception {
//This code is just to show ways to retrieve Http request
and HttpSession
HttpServletRequest request = ServletActionContext.getRequest();
Locale locale = new Locale("hi", "IN");
if(session == null)
this.session = request.getSession(true);
session.setAttribute("org.apache.tiles.LOCALE", locale);
String tmp = (String)request.getParameter("txtUserName");
if(tmp.contains("good"))
return "good";
if(tmp.contains("bad"))
return "bad";
else
return "visitor";
}
}
By setting "org.apache.tiles.LOCALE"session attribute as Locale("hi","IN")
we can use the Locale specific tiles file as "tiles_hi_IN.xml"
The corresponding tiles file is as folows:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://struts.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="good-sample" template="/good.jsp">
<put-attribute name="message" value="Bahut Aacha"/>
</definition>
</tiles-definitions>
So the good.jsp file when called from the struts result typed as "tiles"
good.jsp
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="t1" %>
<font color="green"><b><t1:getAsString name="message"/></b></font>
This page will print on browser as
"Bahut Aacha" (meaning in Hindi as "Very nice")
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,
/struts2-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