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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
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%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

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, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs