December 2008 - Posts

I just want to share a quick code snippet for using Strut's(1.1) bean:message tag with dynamic keys

Assuming you have the following

1. A class named Event with attributes eventId(Long), and myMessageKey(String).

2. myMesssageKey values would come from a database and will have values like label.my.message.

3. You put List of Events in a request-scoped attribute named eventList

You can then iterate through it like this (used inside an html:select):

                     <logic:iterate id="event" name="eventList" scope="request">
                        <option value="<bean:write name="event" property="eventId"/>">
                            <bean:define id="dynamicMessageKey" name="event" property="myMessageKey" type="java.lang.String"/>
                            <bean:message key="<%= dynamicMessageKey %>"/>                            
                        </option>
                      </logic:iterate>                                                                
                  </html:select>

I hope a lot of Struts developer would still find this useful.

Posted by lamia | with no comments