Meaningless Drivel is fun!*
The moose likes Struts and the fly likes Struts 2 list box problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 list box problem" Watch "Struts 2 list box problem" New topic
Author

Struts 2 list box problem

John Cena
Greenhorn

Joined: May 05, 2010
Posts: 6
Hi,

I am new to Struts 2 and I have a list box in which I have to set up some values which are being fetched from my action class now I am able to set up that value but the problem is it is setting up that values as option's ID and value both as below..

<select name="project" id="project">
<option value="Project - 1">Project - 1</option>
<option value="Project - 2">Project - 2</option>
</select>

What I done in jsp is..

<s:select name="project" id="project" label="Select Project" list="%{proj}" />

and in my action class I have 2 ArrayLists
1) proj &
2) projId

What I want is some thing like that ...

<select name="project" id="project">
<option value="1">Project - 1</option>
<option value="2" selected = "selected">Project - 2</option>
</select>

where "1" and "2" are relevant project Ids...


Please help me to find the solution...


Thanks,
Hirav
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Welcome to JavaRanch!

The list passed to the JSP should contain both the ID and text to display.
John Cena
Greenhorn

Joined: May 05, 2010
Posts: 6
Thanks 4 your reply ...

Can you please explain bit further.. please...

I am having 2 Lists mapped in "ArrayList"..

as following

"proj" contains suppose 2 strings "Project - 1" and "Project - 2" and "projId" contains their Ids suppose "1" and "2" and I want "Project - 2" option to be default selected when my page loads.....

and both variables are declared in my action class.

--
Hirav
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Use that. Otherwise the default list box tag won't work: it expects a single list with the appropriate data in it. Don't you already have a project class you're using to hold this information?
John Cena
Greenhorn

Joined: May 05, 2010
Posts: 6
I had mentioned above as well that I am having action class and have declared the arrayList in the same class as well,...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I don't know what else to tell you--the list box expects a single list containing both the option value and text. If that doesn't suit your needs you'll either need to do it manually, or be more descriptive with what you're trying to accomplish.
John Cena
Greenhorn

Joined: May 05, 2010
Posts: 6
Thankx for your reply..

I am copying my pages' code here, that might explain my point to you..

Following is my JSP Page... (CreateThread.jsp)

<s:form action="CreateThread.action" method="POST" >
<s:select name = "project" label="Select Project" list="%{proj}" />
</s:form>

Following is my action class.. (CreateThead.java)

List proj = new ArrayList();
List projId = new ArrayList();
public String execute(){
}

public List getProj(){
proj.add("Project - 1");
proj.add("Project - 2");
return proj;
}
public List getProjId(){
projId.add("1");
projId.add("2");
return projId;
}

Now what I am getting is

<select name="project" id="project">
<option value="Project - 1">Project - 1</option>
<option value="Project - 2">Project - 2</option>
</select>

but what I want in my display page is..

<select name="project" id="project">
<option value="1">Project - 1</option>
<option value="2" selected = "selected">Project - 2</option>
</select>



Please give me some solution ..

Thanks,
Hirav
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I did, but you're choosing to ignore it.

I'll say it again: the select tag expects a *single list*, with *each element* containing *each option's* value and text.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 2 list box problem
 
Similar Threads
Selecting option in second selectOneMenu not setting property value
Struts 1.1: Help needed with html:select, html:options, html:optionsCollection
Select the value from the Combo box
how to use list box in struts and how to retrive values in edit page
Struts 2 Aggregated Lists and Select Boxes