Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can LabelValueBean be resued for different options? - ask help

 
Eli Yang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating web page having multiple rows, each row including a drop down with different option values and labels.

If I have codes like
First dropdown box
<html:select name="someName" property="someProperty">
<html options collection= "LabelValueBean" property="value"></html options>

Second,
<html:select name="someName" property="someProperty">
<html options collection= "LabelValueBeanOne" property="value"></html options>

Third ...
In action class, I save the label values to the different labelvalueBeans,
requst.setAtrribute("LabelValueBean", List);
requst.setAtrribute("LabelValueBeanOne", List);
...

This means I have to creat as many LabelValueBeans as the mumber of dropdown boxes , given them different name, this is NOT a good way to handle this case I believe.

My question is, can I just use one labelvalue bean to handle all the dropdown?

I hope some Master can give me a help, I appreicate any sugestion or any comment.

Thanks!!!


 
alan do
Ranch Hand
Posts: 354
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there's nothing wrong with that approach. it's clean. it maybe cumbersome if you want to reuse these collections for another page or you have to call the same req.setAttribute()'s for multiple methods in a DispatchedAction.

to reuse, you can always store the beans in the session (server performance maybe impacted depending on # of users). if you have to call the same req.setAttribute multiple time, externalize, pull that out into an ActionHelper.generateDropDownBeans() or something.

you can also put a wrapper class around your beans so you'd do only 1 req.setAttribute (but then you'll have to set the beans into local variables in your jsp's).
 
Eli Yang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alan, maybe I should go with using helper class and call only one setAttribute(LabelValueBean, list) in the action class, because based on the page requirement, the number of dorpdown is dynamic, the number is not fixed, the mumber of dorpdown depends on the dada in the session, so I have to find a way to handle this. Could you tell me a more details about set the bean as the variables in jsp and how wrapper class works?

Thanks!

Eli


you can also put a wrapper class around your beans so you'd do only 1 req.setAttribute (but then you'll have to set the beans into local variables in your jsp's).
 
alan do
Ranch Hand
Posts: 354
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi eli,
if you have a dynamic set of dropdowns, then consider putting each collection into a wrapper viewbean, add the viewbeans into an ArrayList, then set that ArrayList in your request (1 req.setAttribute()). in your JSP's then iterate through that ArrayList and display the label and dropdowns. i'll try to explain step-by-step:

1. wrapper class: DropDowns would contains the fields:
String dropDownsLabel; //what to show above each dropdown
String dropDownsKey; //what to use to populate the 'name' of select tag
Collection options;

2. you action then (where appropriate) call a helper class to populate the options (what you already have now).
DropDowns d1 = ActionHelper.populateCollection(some params);
DropDowns d2 = ActionHelper.populateCollection(some params);

ArrayList dropdowns = new ArrayList();
dropdowns.add(d1);
dropdowns.add(d2);

req.setAttribute("dropdowns",dropdowns);

3. i am not sure if you are using the struts-logic or JSTL c library or scriptlets to display your data, but i'll give you the c library example (best way in my opinion).
<c:forEach items="${requestScope.dropdowns}" var="dropdown">
<c ut value="${dropdown.dropDownLabel"/> <br />
<html:select property="${dropdown.dropDownKey}">
<html ptions collections="${dropdown.options}"/>
</html:select>
</c:forEach>

ok, here are some warnings: i've passed JSTL expressions into struts-html tags before, but haven't done it for html:select and html ptions. you can use logic:iterate and localize each dropdowns by doing a <bean:set> for the label, key, and options INSIDE each iteration.

it will take some playing around, but you can work off these steps. good luck.
 
Eli Yang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alan! This is the solution I am looking for!!


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic