Results 1 to 2 of 2
  1. #1
    sunnyjassal is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default JSP, JSF: Cannot find the resource bundle

    Hello,

    I am having a problem with reading a properties file in a web app. I have used servlets in the past but when using JSP and JSF, I am having issues. Basically I am trying to refresh my memory on JSP and learning JSF. For some reason my application is not able to find the properties file. I have the properties file located in the WEB-INF/classes folder. I am also using NetBeans IDE.

    I started a JSF project using a glassfish server from the IDE. I am trying to access a class SystemProperties from JSP page. I get an error that the resource is not found. I have ensured that earth.properties file is in fact in the WEB-INF/classes folder.

    I have used this convention many times using servlets in last several years. It seems like I am doing something really silly or there is something different about JSP/JSF properties file location. Is there a different WEB-INF folder that I should be putting the file in? Please note that I am new to net beans. Any help with this will be greatly appreciated.

    Thanks for your help in advance.

    Error: Can't find the bundle base name earth, locale en_US

    Here is the code:

    package com.earth.system;

    import java.util.Properties;
    import java.io.*;
    import java.lang.*;
    import java.util.*;

    public class SystemProperties {

    protected static ResourceBundle m_resourceBundle;

    static{
    if (m_resourceBundle == null){
    Locale locale = Locale.getDefault();
    ClassLoader loader = ClassLoader.getSystemClassLoader();
    m_resourceBundle = ResourceBundle.getBundle("earth", locale, loader);
    }
    }

    public static String getProperty(String propertyName){
    String value = "";
    if (m_resourceBundle == null) return value;
    try{
    value = m_resourceBundle.getString(propertyName);
    return value;
    }
    catch (MissingResourceException e){
    return value;
    }
    }

    public static String getDatabaseUser(){
    return getProperty("databaseuser");
    }


    JSP File

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@page import="com.earth.beans.*, com.earth.system.*, java.util.*" %>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

    <%
    try{
    out.println(SystemProperties.getDatabaseUser());
    } catch (Exception e){
    //e.printStackTrace();
    out.println("Error");

    }
    %>

  2. #2
    sunnyjassal is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    Hello All,

    I played around with this and found the solution to this. I would just like to share my results for anyone who would or is having the same issue.

    Just a clarification that my SystemProperties is part of a different project that I would like to use in different web applications. In any case I don't think this was part of the issue. After lots of tries I got frustrated and checked to see if I can access the bundle straight from the JSP. Code:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@page import="com.earth.beans.*, com.earth.database.*, java.util.*" %>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
    <f:loadBundle basename="earth" var="msg"/>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

    <f:view>
    <h1>
    <h:outputText value="#{msg.databaseuser}"/>
    </h1>
    </f:view>

    To my surprise this worked flawlessly. This also confirmed that my earth.properties file was in the correct location. After playing around with the code I replaced the following lines in SystemProperties:

    Locale locale = Locale.getDefault();
    ClassLoader loader = ClassLoader.getSystemClassLoader();
    m_resourceBundle = ResourceBundle.getBundle("earth", locale, loader);

    with

    Locale locale = Locale.getDefault();
    m_resourceBundle = ResourceBundle.getBundle("earth", locale);

    Apparently, this worked!!!

    I am intrigued with this solution and would love to get a full understanding. Can anyone explain why this would work?

Similar Threads

  1. Reading a properties file using resource bundle.
    By rudrakanth in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 01:23 PM
  2. tomcat resource bundle tab not appearing??
    By freddieMaize in forum NetBeans
    Replies: 2
    Last Post: 05-25-2008, 10:15 AM
  3. Setting the label of radio buttons from Resource bundle
    By rajeeshankar in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 12-17-2007, 10:23 AM
  4. Archiving old posts into a bundle,FAQ or tutorial
    By cruxblack in forum Suggestions & Feedback
    Replies: 1
    Last Post: 11-14-2007, 01:40 PM
  5. Velocity, cannot find resource
    By sandor in forum Web Frameworks
    Replies: 1
    Last Post: 05-17-2007, 11:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •