Tech I Enjoy Logo

Custom Search




  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
Apache Struts with DOJO Example Part-1 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts 2 Tags Example :
Example of Struts 2 Tags and code explained.
Struts 2 Plugin Example :
Example using Struts 2 Plugin
Apache Struts 2 Tags Example :
Example Steps of using Struts 2 Tags
and code explained.
Apache Struts 2 and XSLT With Example :
XSLT Example using Struts 2 and Example 
code explained.
Apache Struts 2 Example Steps :
Example Steps of using Struts 2 and code explained.
List of Struts Example :
Examples List using Struts 2 and 
code explained.
Apache Struts 2 Validation Example :
Validation Example Steps of using Struts 2
and Example code explained.
Apache Struts 2 Custom Validator :
Example Steps of using Struts 2 with Custom Validator
and code explained.
Apache Struts 2 Result PlainText Example :
Example Steps of using Struts 2 PlainText Result
and code explained.
Apache Struts 2 Radio Tag Example :
Example Steps of using Struts 2 Radio Tags
and code explained.
Struts 2 with JSF Example :
Example using JSF with Struts 2 and 
code explained.
Apache Struts 2 Validation With Example :
Validation Example using Struts 2
and Example code explained.
Struts Validation Example :
Example using Validation with Struts 2 and 
code explained.
Apache Struts Validation with Example :
Example of Validation using Struts and 
code explained.
Apache Struts with DOJO Example Part-2 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts with DOJO Example Part-4 :
Example of DOJO Toolkit with Struts and 
code explained.
Struts 2 Tiles and I18N Example :
Example using Tiles and I18N with Struts 2 and 
code explained.
Apache Struts 2 Validation With Expression :
Validation with Expression using Struts 2
and Example code explained.
Apache Struts 2 Tiles and I18N Example :
Example Steps of using Struts 2 With Tiles
and I18N code explained.
Apache Struts 2 PDF Result :
Example Steps of using Struts 2 with PDF Result
and code explained.
Apache Struts 2 upload Example :
Uploading Example Steps of using Struts 2
and Example code explained.
Apache Struts with DOJO Example Part-3 :
Example of DOJO Toolkit with Struts and 
code explained.
Struts 2 Result Chain Example :
Example using Result Chain with Struts 2 and 
code explained.
Apache Struts Date Validation with Example :
Example of Date Validation using Struts and 
code explained.


References :
Tags: Struts 1 Struts2 plugin
Tags: struts jsf integrate
Tags: struts result chain
Tags: struts tiles i18n
Tags: Struts validation struggle
Tags: Struts
Tags: struts1 date validation
Tags: struts1 struts2 validation
Tags: struts2 dojo 1
Tags: struts2 dojo 2
Tags: struts2 dojo 3
Tags: struts2 dojo 4
Tags: Struts2 example steps Tag usage
Tags: Struts2 example steps
Tags: struts2 own validator
Tags: struts2 pdf result
Tags: struts2 radio tag example
Tags: struts2 result plaintext
Tags: struts2 tags example
Tags: struts2 tiles i18n
Tags: struts2 upload example
Tags: struts2 validation 1
Tags: struts2 validation expression
Tags: struts2 validation
Tags: struts2 xslt article



DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
techienjoy.com is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.

In spite of all precautions taken to avoid any typo in these pages, there might be some 
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.



Android Examples || Android Examples

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions