Tech I Enjoy Logo

Custom Search




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