从位于网站根目录的properties文件中加载和使用JSP文件(使用ResourceBundle 类)没有问题,但是当我试图从位于目录中的JSP加载相同的properties文件时,它会失败,并表示无法找到资源!
位于目录中的页面的代码
<%@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>发布于 2011-07-02 08:51:28
这是因为你不尊重金科玉律:不要在默认的包中放任何东西。资源包作为类从类路径加载。它有一个完全限定的名称,必须使用它来加载它。而且不可能从非默认包中使用默认包中的类。
因此,将您的资源包放在一个适当的包中(例如:com.persiangulfcup.foo.bar),并像这样加载它们:ResourceBundle.getBundle("com.persiangulfcup.foo.bar.labels")。
也就是说,在JSP中使用scriptlet是一种糟糕的做法。您应该真正使用JSTL,它有一个允许使用资源包、格式化消息等的fmt库。
https://stackoverflow.com/questions/6555395
复制相似问题