Advertisement
Home > Struts Struts 2 Example Tutorial > Struts Tiles I18N Example > Code with Sample ActionPlease log in to add or reply to any matter<- requires login or
RMI Example

Home > Struts Struts 2 Example Tutorial > Struts Tiles I18N Example > Code with Sample Action
Struts Tutorials:
Struts2 Tag CheckBoxList
, Checkbox, Iterator, IF
Struts2 Tag Library Example Struts2 Tiles Example Struts2 Tiles I18N Example Struts2 Questions Struts Tiles I18N Example Struts Eclipse MVC Struts2 Tags Struts2 Example and Tutorial Struts MVC Struts2 Validation
Hibernate Tutorials: Hibernate Case Study Class Hierarchy Persist Example Using Hibernate Interceptor Hibernate Questions with Answer Hibernate Many-to-Many Mapping Example Hibernate one-to-many Mapping Example Hibernate and ORM tools Spring Hibernate Example Hibernate SessionFactory Example Hibernate Mapping Class Hierarchy Hibernate Questions Hibernate SessionFactory Questions Spring Hibernate Example: Spring Hibernate Case Study

Written By : Amit
Title :
Struts2 example tutorial
Description : My First example using Apache Struts2
More...


Written By : ISHTEK
Title :
Struts2
Description : Tags such as IF Iterator Tags
More...


Written By : guddu
Title :
Struts Eclipse MVC
Description : Step by step demonstration
More...


Written By : Girish
Title :
Struts Tiles I18N Example
Description : Code with Sample Action
More...


Written By : ISHTEK
Title :
Struts2 questions
Description : open for discussion
More...


Written By : ISHTEK
Title :
Struts2 Tiles Example
Description : with Code and article
More...

Struts, Struts2 Tutorial, Struts 2 Example, Struts2
Author : Girish
Date (Year/Month/Date): 2009-03-17 Struts-Tiles-I18N-Example
References:
Struts2 Tag Library Example
/Struts-Struts-2-Example-Tutorial/Struts2-example-tutorial/My-First-example-using-Apache-Struts2
/Struts-Struts-2-Example-Tutorial/Struts-Tiles-I18N-Example/Code-with-Sample-Action
/Struts-Struts-2-Example-Tutorial/Struts2-Tiles-I18N-Example/with-code-explained-article
/Struts-Struts-2-Example-Tutorial/Struts2-Tiles-Example/with-Code-and-article

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>
    Advertisement
    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).
    Advertisement
    Author of this article/writeup has expressed his/her willingness
    to help or guide users with any technical difficulties he/she faces while working with the example code environment setting up, running and resolving any such exception raised during compile or at runtime. You may ask for any technical doubt or seek technical help related to this article by using following form to reach for technical help from the Author for FREE. This article's Author shall be reading your request and responding within reasonable time (no resolution timeframe defined as such).

    Commented By ->
    Amit
    Nicely explained, but can you provide complete source code
    for this example, so that I would like to run and see it myself.
    
    Thanks in advance, buddy :)

    Commented By ->
    Guddu
    Can I use unicode in Tiles definition XML file as well?
    

    Commented By ->
    Guddu
    Recently I came across a nice web site with lots of insides for
    Internationalization and Localization using Struts and JSF.
    
    Hope this can be useful to readers of this Page:
    Link as follows:
    
    http://www.objectsource.com/j2eechapters/Ch19-I18N_and_L10N.htm

    Commented By ->
    Vane
    I did something like that, but the
    ModuleConfigVerifier throws an ERROR when 
    verifying Forward Configuration, because of the
    missing '/' as firs character on path property.
    
    I think this verification shouldn't be thrown in 
    this case, because we're referencing to a tile 
    instead an URI.
    
    Do you know how can I do to skip this particular 
    verification?
    
    Thanks in advance...
    

    Commented By ->
    Girish
    Hi Vane,
    
    can you show or suggest some pointers towards the present
    example, so as to be able to understand your question properly?
    
    Thanks,
    Girish
    Please write your Comment on this Matter
    (This will be visible if found suitable):
    Name: *
    Email (will not be displayed): *
    Matter: *
    8,17
    Enter bigger number from above :*
    Home > Struts Struts 2 Example Tutorial > Struts Tiles I18N Example > Code with Sample Action
    Visitor/User submitted related resources:
    (Visible upon review and approved by this site Administrator)
    
    Referred By Name *:
    Resource URL *: (e.g, URL should be starting with http://www.-----.---)
     
    Resource Short Description *:
    12,14
    Enter bigger number from above : *

    Please log in to add or reply to any matter<- requires login
    Log in or Register
    This List is generated as on 2009-07-12 (YYYY-MM-DD)
    #Discuss-these : questions-and-answer : Interview-Questions-on-Java #Some-facts-about-MVC-Framework-and-Struts : MVC-Struts : Struts-Struts-2-Example-Tutorial #My-First-example-using-Apache-Struts2 : Struts2-example-tutorial : Struts-Struts-2-Example-Tutorial This article discusses steps of creating a Struts2 based example web application With lots of easy to understand explaination and source code for reference. #Tags-such-as-IF-Iterator-Tags : Struts2 : Struts-Struts-2-Example-Tutorial #Step-by-step-demonstration : Struts-Eclipse-MVC : Struts-Struts-2-Example-Tutorial #Code-with-Sample-Action : Struts-Tiles-I18N-Example : Struts-Struts-2-Example-Tutorial #open-for-discussion : Struts2-questions : Struts-Struts-2-Example-Tutorial #with-Code-and-article : Struts2-Tiles-Example : Struts-Struts-2-Example-Tutorial #with-code-explained-article : Struts2-Tiles-I18N-Example : Struts-Struts-2-Example-Tutorial #Tutorial : Struts2-Tag-Library-Example : Struts-Struts-2-Example-Tutorial #Code-and-Tutorial : Struts2-Tags-Example : Struts-Struts-2-Example-Tutorial #Step-By-Step-Code : Radio-Tag-Example : Struts-Struts-2-Example-Tutorial #Struts-2-Code : Struts2-Validation-Example : Struts-Struts-2-Example-Tutorial #Struts-2-Example-with-Code : Struts2-HibernateSession-Plugin : Struts-Struts-2-Example-Tutorial
    Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved.
    CONTACT    PRIVACY POLICY    DISCLAIMER
    
    This web site provides some of the information about various technologies, example 
    code, tips, tutorials etc. Like any printed matterials, content of these pages may 
    become out of date over a period of time. Therefore all visitor/users of this web 
    site are requested/advised to refer to the originating parties/sources for the 
    latest changes and happenings for detailed information. This information is not 
    intended to be a substitute for the original reference provided by the originating 
    parties/sources.