File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes How to pass drop down box value to a bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to pass drop down box value to a bean" Watch "How to pass drop down box value to a bean" New topic
Author

How to pass drop down box value to a bean

Fred Fasle
Greenhorn

Joined: Oct 22, 2003
Posts: 3
Hello
I have a form with a number of text fields and a drop down box. I am able to pass the values in the text fields into a javabean without any problem using <jsp:setProperty name="myBean" property="*" /> on the following JSP page. But for my drop down box, listed below, I get a number that doesn't match any of the options. I am using simple get and set methods in my Javabean. Any ideas?
<select name="result" size=1 width=40 >
<option value='05'selected>05
<option value='10'>10
<option value='15'>15
<option value='20'>20
<option value='25'>25
</select>
Malhar Barai
Author
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
hi..
Try this..

hth
MB


Malhar Barai
SOA & Java Book
Keith Fiske
Greenhorn

Joined: Jun 27, 2007
Posts: 23
I'm having this exact same problem. I've got properly formed HTML for the select box but the bean value still isn't being set properly. Other text input fields work fine.



Bean setter/getter:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

The original post is from 2003. It is no longer considered acceptable to post to directly to JSP pages. Data handling should happen in task controller servlets.

Is this old code that you are dealing with?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Keith Fiske
Greenhorn

Joined: Jun 27, 2007
Posts: 23
Gah... didn't see the date on it. Just did a search and saw exactly the problem I was having.

No, it's not old code. I was following some examples in the OReilly JSP 3rd edition book. In it, he redirects back to the original form when submitted data does not validate properly. The form is filled out again with the original submitted values using the Bean that is created when the form is submitted.

In his examples, though, I've noticed he conveniently never uses a select dropdown box.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

What year was that published? That's a rather old-fashioned way of doing things.

And to be honest, I don't know what the issue might be. But not having used <jsp:setProperty> in this way since 2001, I may be missing a nuance.

The value of the select element should be sent as part of the request in the exact same manner as other controls. The fact that it's a select should be moot. Have you inspected the HTTP to see if it's passing the value correctly? (With a tool like HttpFox for Firefox.)
Keith Fiske
Greenhorn

Joined: Jun 27, 2007
Posts: 23
http://oreilly.com/catalog/9780596005634/ Mine has 2004 in the book copyright note.

Yes, it is passing the value properly. I tested it using <c:out value="${param.title}" /> on a test processing page and it shows up fine.

Using the <jsp:setProperty ... property="*"> like this on a Bean is supposed to set all properties automatically if it follows the proper bean getter/setter structure, which my bean does. All other text fields pass through perfectly fine, just the select doesn't.

Looking around more, I might have found something. I'm wondering it it's passing it through not as a single value, but some sort of Map or List, since select has the possibility of having multiple values. Not yet sure how I'd get the value then.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

Keith Fiske wrote:http://oreilly.com/catalog/9780596005634/ Mine has 2004 in the book copyright note.

Not quite ancient, but definitely dated.

I'm wondering it it's passing it through not as a single value, but some sort of Map or List, since select has the possibility of having multiple values

How would it know? The request only sees what's passed in the HTTP... how would it know it originated with a select?
Keith Fiske
Greenhorn

Joined: Jun 27, 2007
Posts: 23
Good point. I'm clueless why it's not picking it up then.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

Same here. I'll try to put some time away later to investigate further.

Meantime, why not use the opportunity to explore modern web application structure where you'll never run into this situation. Maybe this article can help you point you in that direction.
Keith Fiske
Greenhorn

Joined: Jun 27, 2007
Posts: 23
Yeah, I saw you link that to someone else in another thread. I bookmarked it to read later. Appreciate your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pass drop down box value to a bean