advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Adding Spice to Struts Part 2 :

Listing 2 represent a pseudo implementation of the CustomMessageResources class and full source code is downloadable from here.

                public CustomMessageResources (List configFiles) {
                                super(null, null);
                                this.bundles = configFiles;
                }
                /** (non-Javadoc)
                 
* @see org.apache.struts.util.MessageResources#getMessage
(java.util.Locale, java.lang.String)
                 */
                public String getMessage(Locale locale, String key) {
                                
                                String message = null;
                                //initialise the variables we need
                                String localeKey = "";
                                if(!super.defaultLocale.equals(locale)){
                                                this.localeKey(locale);
                                }                             
                                //go through all the resource bundle files and load them
                                for (int i = 0; i < bundles.size(); i++) {
                                String config = (String)bundles.get(i);
this.loadBundle(localeKey, config);
//now look if the message can be found
message = (String)resourceMap.get(key);
//message found, so get out of the loop
if(message !=null){
break;
}else{

System.out.println("COULD NOT FIND RESOURCE FILE VALUE FOR KEY: "+key);
}
}
return message;
}   

    /**
     * Loads the resource bundle based on the locale.
     * @param localeKey The locale
     * @param config The resource file.
     */
protected void loadBundle(String localeKey, String config) {

        String fileName = null;
        InputStream inStream = null;
        Properties props = new Properties();
        //first check if this bundle is alreay loaded previously
        if (bundlesLoaded.get(config) != null) {
return;
        }
        //otherwise mark this bundle being loaded
        this.bundlesLoaded.put(config, localeKey);
        // Set up to load the property resource for this locale key, if we can
        fileName = config.replace('.', '/');
        if (localeKey.length() > 0) {
            fileName += "_" + localeKey;
    }
    fileName += ".properties";

    //obtain a class loader
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    //if this classloader is null, use the class loader used to load this class
    if(classLoader ==null){
        classLoader = this.getClass().getClassLoader();
    }    
    //load the file as Stream
    inStream = classLoader.getResourceAsStream(fileName);      
    //check if the Stream in not null
    if(inStream!=null){
        //populate the properties from this input Stream
    try {
        props.load(inStream);
    } catch (IOException e) {
       // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
    try {
        inStream.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    }
        
        //now retrieve the key value pairs from the resource file and 
        //put it in the map
        synchronized(this.resourceMap){
Iterator iterator = props.keySet().iterator();

        while(iterator.hasNext()){
          String key = (String)iterator.next();
          //String messageKey = this.messageKey(localeKey,key);
          String value = props.getProperty(key);  
            this.resourceMap.put(key,value);
}
}          

                }else{
                                  
 System.out.println("COULD NOT LOAD RESOURCE FILE: "+fileName);
                                }
                   
                             
                }
Listing 2: Pseudo implementation of the CustomMessageResources class

Once we have this new CustomMessageResources class, the only thing left to do is to make use of it. We have overriden the Struts ActionServlet class to make use of the new CustomMessageResources class. We have decided that we will specify the message resource files in a comma separated manner within the struts-config.xml file. Here is how they should look:

<message-resources null="false" parameter="page-resources,error-resources"/>

Here is all we need to get us going. In a nutshell, we have a new CustomMessageResources class, which parses through a list of resource bundle files and loads them. The overridden ActionServlet class stores this CustomMessageResources class instance against any config key specified in the configuration or else stores against a default key.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 34
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

IBM Brings Developers Into the Cloud
Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs