You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Giuseppe Galli <gi...@agora.it> on 2000/07/26 13:25:23 UTC

Re: Loading resource dynamically

Hi alll,
just to say that you have several way to load resource dinamically, each
suitable for different situation.
If you need to load the month names in a localized manner, put your code in a
jsp file called init.jsp that
creates through <jsp:usebean a bean (with 'application' value as scope
attribute). this bean would  dinamically loads the data from the resource
file.If this file is preloaded, every jsp page and servlet in your web app can
use the bean getXXX methods, also the getMonth() method that gives you the value
'6' to put here: <struts:select property="activationMonth" value="6">

Hope this helps
Pino.


Chris Miller wrote:

> I have the following strings declared in my resources.properties file:
>
> month.1=January
> month.2=February
> ... and so on.
>
> I am currently then doing the following in my JSP's:
>
> <%-- Load the month names --%>
> <%
>   MessageResources resources = (MessageResources)
> config.getServletContext().getAttribute("org.apache.struts.action.MESSAGE");
>
>   // Load in the month value/label pairs
>   String[][] months = new String[2][12];
>   for (int i = 0; i < 12; i++) {
>     months[0][i] = String.valueOf(i + 1);
>     months[1][i] = resources.getMessage("month." + String.valueOf(i + 1));
>   }
>   pageContext.setAttribute("monthValues", months[0]);
>   pageContext.setAttribute("monthLabels", months[1]);
> %>
>
> Then further down I'm doing something like...
>
> <struts:select property="activationMonth" value="6">
>   <struts:options name="monthValues" labelName="monthLabels"/>
> </struts:select>
>
> ...to dynamically populate the month names on a form.
>
> This works fine. However, I would like to move the code looking up the month
> names out into a .java file (to clean things up, and so that all my pages
> can call the same code). What I'm not quite sure on is, do I have to move
> this into a servlet? Or can I use a bean? I think a bean would be more
> suitable(?), but then how do I access the MessageResources - would I have to
> pass the 'config' object to the bean each time I wanted to get the month
> array? Any ideas on the best way to set this up would be appreciated! I'm
> new to all this Java/JSP/Servlet stuff, so I'm not quite sure of the best
> approach.
>
> Thanks,
>
> Chris Miller