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 Can LabelValueBean be resued for different options? - ask help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Can LabelValueBean be resued for different options? - ask help " Watch "Can LabelValueBean be resued for different options? - ask help " New topic
Author

Can LabelValueBean be resued for different options? - ask help

Eli Yang
Greenhorn

Joined: Apr 17, 2005
Posts: 3
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

Joined: Apr 14, 2005
Posts: 354
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).


-/a<br />certified slacker...yes, my last name is 'do' - <a href="http://www.luckycouple.com" target="_blank" rel="nofollow">luckycouple.com</a>
Eli Yang
Greenhorn

Joined: Apr 17, 2005
Posts: 3
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

Joined: Apr 14, 2005
Posts: 354
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

Joined: Apr 17, 2005
Posts: 3
Thanks Alan! This is the solution I am looking for!!


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can LabelValueBean be resued for different options? - ask help