Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts 2 list box problem

 
John Cena
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

The list passed to the JSP should contain both the ID and text to display.
 
John Cena
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic