aspose file tools*
The moose likes Struts and the fly likes How to get selected value from struts dropdown list? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to get selected value from struts dropdown list?" Watch "How to get selected value from struts dropdown list?" New topic
Author

How to get selected value from struts dropdown list?

Jonilie Echavez
Greenhorn

Joined: Jun 26, 2009
Posts: 20
Hi,

I have a bean class called Parameter, which is defined by the following attributes:

public class Parameter implements Serializable
{
private final String name;
private String value;
private List paramOptions;

...constructor and getter and setter...
}

In my form, paramOptions shows as a dropdown list.

<nested:notEmpty name="parameterList" property="paramOptions">
<html:select name="parameterList" property="value" >
<bean:define name="parameterList" property="paramOptions" id="pList"/>
<htmlptions collection="pList" property="paramOptionName" labelProperty="paramOptionLabel" />
</html:select>
</nested:notEmpty>

1)I am passing the bean with the property called value which is empty. But the paramOptions is a List.
However, when i submit the form, i want to pass the value selected in the dropdown list to the property called value. Is it possible to do this?

2)how do i get the item selected from the dropdown list?

Please help.

THanks in advance.


Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Jonilie Echavez wrote:However, when i submit the form, i want to pass the value selected in the dropdown list to the property called value. Is it possible to do this?

Had you tried it ? Generally, when you "select" an "option", that's options value is get submitted when you SUBMIT the FORM, so I'm curious to know whats happening in your case, any error/exception ?

Also do not use property name as "value", its ambiguous with the "attribute" value .

And use CodeTags


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Jonilie Echavez
Greenhorn

Joined: Jun 26, 2009
Posts: 20
Hi, thanks for the reply.

When i get the value for my option, it gets the List, not the value of the item selected.

In the Action page, i get the paramOptions like this:

Output:
Attribute paramOptions:[report.ParamOptions@daa6d1, report.ParamOptions@fde3c7]


I then change the code a bit to assign an id called paramSelected:



Then in the Action form i want to display again the output, trying different ways:



But i don't get the value that was selected:

Attribute getAttribute paramSelected:null
Attribute param.getValue:
Attribute paramOptions:[report.ParamOptions@daa6d1, report.ParamOptions@fde3c7]


Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Jonilie Echavez wrote:In the Action page, i get the paramOptions like this:

Output:
Attribute paramOptions:[report.ParamOptions@daa6d1, report.ParamOptions@fde3c7]


param.getParamOptions(), returns an array of objects of type 'report.ParamOptions', so what you really need to make it print something readable, is either override 'Object#toString()' method OR us a method which prints the value, something like this
Jonilie Echavez
Greenhorn

Joined: Jun 26, 2009
Posts: 20
Hi,
But how do i know which index from the list was selected. The code below means i always pick the value on the first item of the dropdown?

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

After consulting the Struts taglib docs,
http://struts.apache.org/1.3.10/struts-taglib/tagreference.html#html:select

for this code

Try following code in Action class.

deepika bhatia
Greenhorn

Joined: Jun 26, 2009
Posts: 10
Hi,
What you are getting is the whole list in the drop down,
you should pass thsi list to service class from the action class and using List Collection,you should extract each element of the list in a variable using a for loop and then you can use your specific value.
I think this would help
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

deepika bhatia wrote:
you should pass thsi list to service class from the action class and using List Collection,you should extract each element of the list in a variable using a for loop and then you can use your specific value.

No, that's not the original poster seeks, though it will print the whole list but not the "selected<option>" value.

And Welcome to JR
Jonilie Echavez
Greenhorn

Joined: Jun 26, 2009
Posts: 20
The thing is if I make the property as an input text, i am able to get the value.

JSP:

In Action Form:

Output:
Attribute param.getValue:joni

But in a select, where the values in the dropdown are from another property paramOptions, and i assign the same value for name and property, I am not able to get the value selected.

JSP:

The output when i get the property "value" is an empty string, which is what it is initialized as when the jsp page is rendered.
Output:
Attribute param.getValue:
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Actually I asked you try something, which I don't see in your latest reply. Second If you read this link carefully, you can get a hint abt how to go for it.

http://struts.apache.org/1.3.10/struts-taglib/tagreference.html#html:select
Jonilie Echavez
Greenhorn

Joined: Jun 26, 2009
Posts: 20
Sagar, i didn't get to post my reply.

This is the result when the value is entered, the request.getAttribute("value") was null.
>>Attribute param.getValue:joni
>> request.getAttribute value: null

This is the result when the value put in dropdown, the request.getAttribute("value") was also null.
>>Attribute param.getValue:
>>request.getAttribute value: null

Checking on the doc link...
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Jonilie Echavez wrote:This is the result when the value put in dropdown, the request.getAttribute("value") was also null.
>>Attribute param.getValue:
>>request.getAttribute value: null

I asked you this, because this is what I read from the given link
property* :
Name of the request parameter that will be included with this submission, set to the specified value.


Now if that returns "null" concentrate on this, from the same link

name :
The attribute name of the bean whose properties are consulted to determine which option should be pre-selected when rendering this input field. If not specified, the bean associated with the enclosing <html:form> tag is utilized.



Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507


Can anyone tell me the solution for this...???
how is this resolved ??
Solution provided will be highly apprciated


When The Going Gets Tougher,The Tougher gets Going
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get selected value from struts dropdown list?