What is the best possible way to create a jsp table(key,value) from a properties file.
Right now I am doing this using scriptlets…..
ResourceBundle statusCodes = ResourceBundle.getBundle("statuscode");
Enumeration statusKeys = statusCodes.getKeys();
<%
while (statusKeys.hasMoreElements()) {
String key = (String) statusKeys.nextElement();
String value = statusCodes.getString(key);
%>
<tr>
<td><%=key%></td>
<td><%=value%></td>
</tr>
NOTE: Dont worry about syntax this is not complete code.
How can I do this using EL and jstl
You should be using
java.util.Propertiesinstead ofjava.util.ResourceBundle. TheResourceBundleserves an entirely different purpose and it should not be abused to have “an easy way” to load properties since it by default lookups resources from the classpath.Let a servlet load and prepare it for JSP.
Because
Propertiesimplementsjava.util.Map, you can just use JSTL<c:forEach>to iterate over it. Every iteration gives aMap.Entryback which in turn hasgetKey()andgetValue()methods.Finally invoke the servlet by its URL to get it to display.
Please note that
ResourceBundledoesn’t implementjava.util.Map!