• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drop Downs with Properties File ?

 
Annmarie Ziegler
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Annmarie Ziegler
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to create arrays dynamically as Jason has mentioned in his code?
 
john doms
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic