You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Chris Miller <ki...@vardus.co.uk> on 2000/07/24 17:17:26 UTC

Loading resources dynaimcally

One further thing - if I want to have my page select the current month by
default, I need to dynamically do something like:
<%
  Calendar now = Calendar.getInstance();
  String month = String.valueOf(now.get(Calendar.MONTH));
%>

... and then ...

<struts:select property="activationMonth" value="<%=month%>">
  <struts:options name="monthValues" labelName="monthLabels"/>
</struts:select>

But how do I set 'value' for the struts:select tag? <%=month%> obviously
doesn't work. Can I read it off a property on a bean? The docs don't really
cover this - is it even possible? Or is there a work-around?

PS. Sorry - I screwed up the subject on my last email!)


> 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
>
>