JSP resourceBundle

15,142

This is because you didn't respect a golden rule: don't put anything in the default package. A resource bundle is loaded as a class, from the classpath. It has a fully qualified name, which must be used to load it. And it's not possible to use a class in the default package from a class not in the default package.

So, put your resource bundle in an appropriate package (example : com.persiangulfcup.foo.bar), and load them like this : ResourceBundle.getBundle("com.persiangulfcup.foo.bar.labels").

That said, using scriptlets inside JSPs is a bad practice. You should really use the JSTL, which has a fmt library allowing to use resource bundles, format messages, etc.

Share:
15,142
ehsun7b
Author by

ehsun7b

I love computer programming, music and animals.

Updated on June 04, 2022

Comments

  • ehsun7b
    ehsun7b almost 2 years

    I have no problem loading and using properties file from JSP files which are located in the root of website (using ResourceBundle class) but when I try to load the same properties file from a JSP which is located in a directory it fails and says the resource can not be found!

    Code of the page which is located in a directory

    <%@page import="org.apache.log4j.Logger"%>
    <%@page import="com.persiangulfcup.utility.LogUtils"%>
    <%@page import="java.util.ResourceBundle"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <%
      Logger logger = LogUtils.getLogger("page/contact");
      ResourceBundle lbls = null;
      ResourceBundle msgs = null;
      try {
        lbls = ResourceBundle.getBundle("labels");
        msgs = ResourceBundle.getBundle("messages");
      } catch (Exception ex) {
        logger.fatal(ex);
      }
    %>
    <div class="form">
      <div style="text-align: left; font: normal bold 14px arial; cursor: pointer" onclick="contactBox.hide();">X</div>
      <div style="padding-bottom: 10px;font-size: 14px; text-align: center"><%=msgs.getString("contactHeader")%></div>
      <form id="frmContact" onsubmit="try {sendContact();} catch (e) {console.log(e)} return false;">
        <table class="form">
          <tr>
            <td class="caption"><%=lbls.getString("name")%>: </td>
            <td class="data">
              <input id="txtName" type="text" name="txtName"/>
            </td>
          </tr>
          <tr>
            <td class="caption"><%=lbls.getString("email")%>: </td>
            <td class="data">
              <input id="txtEmail" type="text" name="txtEmail"/>
            </td>
          </tr>
          <tr>
            <td class="caption"><%=lbls.getString("subject")%>: </td>
            <td class="data">
              <input id="txtSubject" type="text" name="txtSubject"/>
            </td>
          </tr>
          <tr>
            <td class="caption"><%=lbls.getString("message")%>: </td>
            <td class="data">
              <textarea id="txtMessage" name="txtMessage"></textarea>
            </td>
          </tr>
          <tr>
            <td class="button" colspan="2"><input type="submit" value="<%=lbls.getString("send")%>"/></td>        
          </tr>
          <tr>
            <td style="text-align: center" colspan="2" id="brdContact"></td>        
          </tr>
        </table>
      </form>
    </div>
    
  • ehsun7b
    ehsun7b almost 13 years
    is it applicable for log4j properties too? And the pages in the root are like classes in the default package?
  • JB Nizet
    JB Nizet almost 13 years
    The default place for log4j.properties is in the default package, but they're simple properties, not rersource bundles, and are thus not loaded through the same mechanism. Regarding the place of JSPs in the root, I think it depends on the server used, which chooses how to generate the corresponding classes.
  • ehsun7b
    ehsun7b almost 13 years
    is it correct now? ResourceBundle lbls = ResourceBundle.getBundle("com.persiangulfcup.config.labels")‌​;
  • JB Nizet
    JB Nizet almost 13 years
    If the labels.properties file is in the com.persiangulfcup.config package, then yes, it's correct.