wood burning stoves*
The moose likes Struts and the fly likes how i get selected list data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how i get selected list data" Watch "how i get selected list data" New topic
Author

how i get selected list data

Raj
Greenhorn

Joined: Dec 05, 2007
Posts: 17
Hello,
This is my code:

ArrayList<PS_Department> UsersDept = serverFunc.adminLib.GetDepartmentsByCurrentUser();
int[] selectedDepts = new int[UsersDept.size()];
int i = 0;
for (PS_Department dept : UsersDept) {
selectedDepts[i]= dept.getID();
i++;
}

theForm.setSelectedDeptIds(selectedDepts);
here i got all the department value As a selected value.but i want only those selected value which is selected by user.how can i got those selected value.
Please help....
Nick Williamson
Ranch Hand

Joined: Jan 06, 2007
Posts: 73
not sure I fully understand what you want to accomplish:

first off to create an array from a list is as simple as this:
list.toArray(); that will create an object array for you... API = your friend.

When the form is submitted it will submit the values checked so your jsp. First before you load the jsp put your complete ArrayList in the request and iterate over that to create your checkboxes in your jsp and give the checkboxes their value.

jsp code


once you submit, your form's array will be populated with all the ids that have been checked and you can do your processing from there.
Raj
Greenhorn

Joined: Dec 05, 2007
Posts: 17
Thanks for the reply.
Actually i am using MultiSelect List Box.And I got all the value as a selected in my jsp page.

here is my code.
ArrayList<PS_Department> UsersDept = serverFunc.adminLib.GetDepartmentsByCurrentUser();
int[] selectedDepts = new int[UsersDept.size()];
int i = 0;
for (PS_Department dept : UsersDept) {
selectedDepts[i]= dept.getID();
i++;
}
After ittrating i got all the value as a selected value in my jsp page.
is there any way to check only those selected value?
Nick Williamson
Ranch Hand

Joined: Jan 06, 2007
Posts: 73
same concept, you need to populate the values of the select with the list, don't set the values on the form. What you are doing, you're setting the values on the form so when the page loads all options will be selected.

1. DO NOT create an array from the list and set it on the form!
2. The array is going to be data filled from the form submit DON'T SET IT
3. Your select tag should use a list in the request or on the form, not the array from the from, that array is only to capture what is sent when the form is submitted.

Raj
Greenhorn

Joined: Dec 05, 2007
Posts: 17
Thanks again.
In my action class i want to put one condition so that it will store only selected value rather than all value.
my code
ArrayList<PS_Department> UsersDept = serverFunc.adminLib.GetDepartmentsByCurrentUser();
int[] selectedDepts = new int[UsersDept.size()];
int i = 0;
for (PS_Department dept : UsersDept) {
selectedDepts[i]= dept.getID();
i++;
}

see after for loop i got all the value and store in SelectedDepts[i].So here SelectedDepts[i]i want to store only those value which is selected.Please help.
Nick Williamson
Ranch Hand

Joined: Jan 06, 2007
Posts: 73
I have no idea what you are talking about anymore. From your original question it makes no sense, the thing I have shown you should work the way you want. The way you get a list from the user is to have them submit the form to your action and then your array would only have the ids that were selected. I can't help you anymore with the information you have given. I see your code and that doesn't make sense to me either, all you are doing is creating an array of ids that are in your original list. That doesn't make sense. you have to have something to compare it to.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Rajm: I will give you some leeway on your English and I am sure that it was a mistake that you posted several threads with this same question. Nick is trying to help out, but posting what looks like the same few lines of code 3 times is not helping anybody understand your question. Step back, think about what you are trying to do and then post a follow up to this thread that does not contain any code.

- Brent
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how i get selected list data