Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get selected value from struts dropdown list?

 
Jonilie Echavez
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jonilie Echavez
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can anyone tell me the solution for this...???
how is this resolved ??
Solution provided will be highly apprciated
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic