JSP resourceBundle
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.
Comments
-
ehsun7b almost 2 years
I have no problem loading and using
properties
file fromJSP
files which are located in the root of website (usingResourceBundle
class) but when I try to load the sameproperties
file from aJSP
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>
-
BalusC almost 13 yearsEeeek, scriptlets. Use JSTL fmt. stackoverflow.com/questions/4276061/…
-
-
ehsun7b almost 13 yearsis it applicable for log4j properties too? And the pages in the root are like classes in the default package?
-
JB Nizet almost 13 yearsThe 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 almost 13 yearsis it correct now? ResourceBundle lbls = ResourceBundle.getBundle("com.persiangulfcup.config.labels");
-
JB Nizet almost 13 yearsIf the labels.properties file is in the com.persiangulfcup.config package, then yes, it's correct.