File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Drop Downs with Properties File ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Drop Downs with Properties File ?" Watch "Drop Downs with Properties File ?" New topic
Author

Drop Downs with Properties File ?

Annmarie Ziegler
Greenhorn

Joined: Apr 06, 2001
Posts: 21
Hi all,
I'm rather new to Struts and I'm having trouble figuring out how to populate a drop down list dynamically using a properties file. I have been searching for an example, but I haven't had any luck locating one? Does anyone have an example or a link?
I am trying to populate two drop-down lists - one for country and the other for state/province - with the country list being the master for the state list. Does it make sense to populate the lists from a properties file or should I load the properties file (country/state) values in to beans and access them as normal? Any advice?
Thanks in advance for I really appreciate the help.
Annmarie
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
I would load the data into beans or some collection (or a collection of beans). For one thing, this is static data and you'll probably only want to load it once for the entire application, preferably at start time (such as via a Strits plugin). I personally wouldn't populate the dropdown lists directly from a properties file.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Maybe this bit of html/javascript will help you out:

You'll notice in the javascript that the arrays are hardcoded. You would want to populate this from your beans or properties file. A scriptlet with out.println statements that construct your arrays based on the contents of your beans should suffice. In other words, you're dynamically writing the JavaScript that allows the whole thing to work.
Something else to notice is the html for the "cities" list. You'll see that I have four empty option tags. You will need to dynamically create the same number of these tags as the maximum size of data that will be used to populate this list. In the case of my example, you'll see that max size is four.
HTH
[ April 12, 2004: Message edited by: Jason Menard ]
Annmarie Ziegler
Greenhorn

Joined: Apr 06, 2001
Posts: 21
Thanks Jason.
I've already taken the steps to populate beans from the property files ... I didn't think loading directtly from a property file to the page was a good idea - I'm glad you agree.
I will create a state string that is comma delimited - one per country, and add it to a session bean. I was thinking of using the following snippet, but will need to alter it a bit to handle the country selection change. The javascript you posted will come in handy for that portion.
<%int i = 1;%>
<c:set var="str" ><bean:write name="countryStates" property="usstates"/></c:set>
<html:select property="states">
<html ption value="" ><fmt:message key="select" /></html ption>
<c:forEach var="str" items="${str}">
<html ption value="<%=Integer.toString(i)%>" ><fmt:message key="${str}" /></html ption>
<%i++;%>
</c:forEach>
</html:select>

Thanks again for the help!
Annmarie
Dave Knipp
Ranch Hand

Joined: Oct 14, 2003
Posts: 146
I definitely think properties files are not the way to go. Populate a list with ListValueBean's in it, like this.

The label and value are the strings you want to represent the option's label and value. You can then place that list (collection) in the request and display the list elements in three lines:

Check out this link for more: http://www.husted.com/struts/tips/index.html
Dave


SCJP 1.4, SCJD 1.4, SCWCD 1.3, SCBCD 1.3, IBM Certified Solution Developer -WebSphere Studio V5.0
Annmarie Ziegler
Greenhorn

Joined: Apr 06, 2001
Posts: 21
Thanks Jason.
I've already taken the steps to populate beans from the property files ... I didn't think loading directtly from a property file to the page was a good idea - I'm glad you agree.
I will create a state string that is comma delimited - one per country, and add it to a session bean. I was thinking of using the following snippet, but will need to alter it a bit to handle the country selection change. The javascript you posted will come in handy for that portion.
<%int i = 1;%>
<c:set var="str" ><bean:write name="countryStates" property="usstates"/></c:set>
<html:select property="states">
<html ption value="" ><fmt:message key="select" /></html ption>
<c:forEach var="str" items="${str}">
<html ption value="<%=Integer.toString(i)%>" ><fmt:message key="${str}" /></html ption>
<%i++;%>
</c:forEach>
</html:select>

Thanks again for the help!
Annmarie
Rashid Darvesh
Ranch Hand

Joined: Feb 13, 2004
Posts: 189
i amhaving problems with the html options tag i am having a namecode bean which has two properties. here is the code
<%
NameCode nc=null;
List yearL = new ArrayList();
nc=new NameCode();
nc.setCode("K0009");
nc.setName("2002");
yearL.add(nc);
nc=null;
nc=new NameCode();
nc.setCode("K000010");
nc.setName("2003");
yearL.add(nc);
request.setAttribute("YEARS",yearL);
%>
and i am using in hte struts tag as
<html:select property= "years">
<html options collection="YEARS" property="code" labelProperty="name" />
</html:select>
Could anyone please tell me what's wrong with this code
Thanks...
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Hi Rashid,
Why not post your question in its own topic, and I'm sure it will make it easier to find someone to help you out. It will also keep people who are trying to help Annmarie from getting too confused.
Sangeetha Karunanithi
Greenhorn

Joined: Jun 22, 2005
Posts: 1
How to create arrays dynamically as Jason has mentioned in his code?
john doms
Greenhorn

Joined: Jan 02, 2006
Posts: 1
Originally posted by Jason Menard:
Maybe this bit of html/javascript will help you out:



You'll notice in the javascript that the arrays are hardcoded. You would want to populate this from your beans or properties file. A scriptlet with out.println statements that construct your arrays based on the contents of your beans should suffice. In other words, you're dynamically writing the JavaScript that allows the whole thing to work.

Something else to notice is the html for the "cities" list. You'll see that I have four empty option tags. You will need to dynamically create the same number of these tags as the maximum size of data that will be used to populate this list. In the case of my example, you'll see that max size is four.

HTH

[ April 12, 2004: Message edited by: Jason Menard ]


"Is it possible to have a link of each cities? just asking for help coz i am planning to put it in my site - many thanks"
 
jQuery in Action, 2nd edition
 
subject: Drop Downs with Properties File ?