aspose file tools*
The moose likes JSP and the fly likes Using html drop down box to retain values (in a jsp page) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Using html drop down box to retain values (in a jsp page)" Watch "Using html drop down box to retain values (in a jsp page)" New topic
Author

Using html drop down box to retain values (in a jsp page)

dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
Hi, I am encountering a problem where the value in the drop down resets to the very first value in the drop down (passed) every time i click the submit button. Also, it does not retain the value when I come back any time on the page. However, I can see that the database is being populated with the value I chose. I have the following code below.
How can I make it so that every time I click the submit button, the value is remembered in the drop down and does not reset back to the first value in the list....Let me know if you need more code.

In JSP


In Servlet


In the status choices i have


In the bean i have


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

This has been discussed many times, please SearchFirst.

You must put the selected attribute on the option you wish to be selected.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
I know, believe me I've looked at other postings and I've tried to model that in my code. However, it's not working and I'm really stuck. I do have the 'selected' attribute (in line 6 of the first code box). Everything makes sense but for some reason it's always going back to the first value in the drop down after I click submit. Any help would be appreciated.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Then you need to look at what the HTML being sent to the browser is, and then figure out why it is not working.

Once the HTML is correct, it will do what you want.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

do you use firefox? if yes then use firebug for debugging on client side. it save lot of time for you!
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
Bear Bibeault wrote:Then you need to look at what the HTML being sent to the browser is, and then figure out why it is not working.

Once the HTML is correct, it will do what you want.


You mean the ${item.value}? What that value is returning?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

No, I mean you need to look at the HTMl that is being sent to the browser. Once you know what's wrong with the HTML, you should be able to figure out why it's not correct.
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
I'm not sure which part exactly you're referring to. Do you mean the jsp or the servlet? Can you please be more specific...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18140
    
    8

What is not specific about "the HTML which is sent to the browser"?

Your JSP is generating HTML, right? So when you see that HTML in the browser, you right-click on it and use the "View Source" option (or something like that, depending on your browser).

Then, as Bear said, figure out what is wrong with that HTML and what should it be instead. Finally write your JSP to generate the HTML which it should be.
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
Well, I took a look at the source and I just can't figure out why there is not 'selected' attribute next to any of the options...and I have specified 'select' in the jsp code above and I am sure the database is being populated correctly.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Now you know that there's a logic problem with the code that should be adding the selected attribute to the option.

Start debugging that.
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
Honestly, I'm really stuck...the below code seems fine to me. Not really sure where to go from here....

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Debugging 101:

The problem is most likely with the expression ${item2.value == person.hcsoScreening ? 'selected' : ''} as you are not getting the selected value where you expect it.

So rather than just staring at it perplexedly, what can you do to find out what's going wrong? What is the most likely cause of the expression not being evaluated as you expect?
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
The problem is that the 'person.hcsoScreening' is not populating correctly....but I don't understand why? I have the line below in the code and all of the other attributes associated with person get populated. i.e. person.firstName...


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Then it's time to back-track and find out where you think the property should be being set, but isn't.
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
The person.hcsoScreening is being populated though....I did a test outside of that select (below) and it returned a value. It's only inside the <c:forEach> statement that it's not returning a value....any ideas on why that is

My test that returns a value
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

That is not possible. Check your typing/spelling.
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
I did, but the hcsoScreening in the line below doesn't return a value...otherwise it would evaluate the expression correctly



However, if I put in a hard code value in place of person.hcsoScreening, for example 'failed' (like the example below) then it WORKS....So this leads me to believe that hcsoScreening is not returning a value...

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Not buying it. If the value evaluates properly elsewhere there is no reason it can just stop. There is something else at play here.
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
I swear to you it's true...the cut evaluation returns a value (failed, passed, etc) and the expression below it does not...it always goes to the default 'passed' which is the first on in the list. does someone have any ideas on this? If so, please help me out...going insane here.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18140
    
    8

Debugging 101: if you want to know what's in a variable at some point in time, print it out.

So instead of this:


do this:


and let's see what is in the HTML.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

If I could give Paul's post 10 thumbs-up I would. This is something you should have tried already.
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
Paul, unfortunately I did not know you can do this (what you suggested). I see the output below
Does this mean that I have to reset a value...it looks like it's saving it in a big string?? How do I reset that

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18140
    
    8

If I understand that right, the value of
${person.hcsoScreening}

is "not started/passed " (with a whole lot of trailing spaces). And yes, that isn't equal to any of the four options (even disregarding the trailing spaces), and so therefore you don't see any of them being "selected" in the HTML.

I don't know what you are talking about when you say "reset a value". Apparently you were expecting the value of that hcsoScreening attribute to match one of the selectable options. But it doesn't. So you're going to have to do something different. Since I don't know anything about your business rules regarding that attribute, I have no idea what.
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
Ok, so in order for the expression to work properly, I somehow need to achieve the output below...correct? I'm not sure why there would be trailing spaces. Could that be a database issue? I'm using DB2. Could it be that the fields themselves are a set amount of characters and it's taking them into account? Really appreciate your help


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18140
    
    8

dekij ra wrote:Ok, so in order for the expression to work properly, I somehow need to achieve the output below...correct?

Not according to what you've been saying all along. You wanted one of the options to be selected, right?
I'm not sure why there would be trailing spaces. Could that be a database issue?

I wouldn't call it a database issue. If you don't want them, there's a String method to get rid of them. But even if you did get rid of them, that still doesn't change the fact that the value doesn't match any of your options.
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
Yes, I'd like one of them to be selected (the previously selected value). So what would be an exmaple which has one of them selected? So you don't think that the extra spaces are coming from a database where the fields are CHAR(50)? So, the problem is arising from the fact that there are extra spaces in there as well as other values added to that field? I still don't understand how those extra values are coming into play, since I see only one value in my cut test...this is really bizzare
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18140
    
    8

dekij ra wrote:Yes, I'd like one of them to be selected (the previously selected value). So what would be an exmaple which has one of them selected?

I was under the impression that you knew how to generate HTML with one of the options selected. We've been talking about that HTML for the whole thread. If you really don't know then it's not even worth continuing with the thread.
So you don't think that the extra spaces are coming from a database where the fields are CHAR(50)? So, the problem is arising from the fact that there are extra spaces in there as well as other values added to that field?

Those extra spaces are quite likely coming from such a field. And I specifically said in my last post that I didn't think the extra spaces were the problem. The problem is this, to spell it out in tedious detail:

The field you are comparing against is "not started/passed".

It is not equal to "passed".

It is not equal to "failed".

It is not equal to "not started".

It is not equal to "n/a".

Those things are all true regardless of whether trailing spaces are present or not.
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
Right, I understand that I have to use the 'selected' attribute....I guess I formulated my question poorly..Anyways, I think I know what the problem is. Every time that a value gets changed by the drop down it gets inserted into that database field where the person.hcsoScreening reads from. That is probably the reason why there are other values in the field. The question now is why is it adding to the database field rather than just updating it (because I specifically have an update query, not insert).
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18140
    
    8

Okay, so there you go. You have something to look for now.
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
Ok, I officially know the cause of the problem. It is actually the spaces!!! I did a couple tests and it seems that the extra spaces in there are the cause of the problem. Paul, I know that you mentioned there's a way to get rid of them? Is there a way I can do this in the jsp itself? Like some sort of function to call upon the person.hcsoScreening attribute to remove all extra spaces? You've been a tremendous help! Really appreciate it. I was going in circles literally for three days. But I guess that's what happens when you're learning
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18140
    
    8

To trim the whitespace from a string? In a JSP? Why yes, there's a JSTL function to do exactly that. Check out your JSTL docs.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

dekij ra wrote:Is there a way I can do this in the jsp itself?

Yes, but it's highly recommended to handle things like data cleansing in the controller, not the JSP.
dekij ra
Ranch Hand

Joined: May 21, 2010
Posts: 37
Ok, for anyone who's interested and struggling with this same problem, this is the function ---> fn:trim(valueToTrim)
and also don't forget to include the library <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

Agreed Bear, but I'm under a deadline so I need to go with the quickest way.

Thanks all for you help


Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11279
    
  59

dekij ra wrote:So you don't think that the extra spaces are coming from a database where the fields are CHAR(50)? So, the problem is arising from the fact that there are extra spaces in there as well as other values added to that field? I still don't understand how those extra values are coming into play, since I see only one value in my c:out test...this is really bizzare


When you post here, there is a checkbox just below the area you type in that allows you to turn off ubb code - please use it so that c:out is not rendered as cut.

I recommend you read up on the difference between char and varchar


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using html drop down box to retain values (in a jsp page)
 
Similar Threads
What to do to make this web page thread safe?
Retain drop down values in JSP
Dropdown Selected returning unexpected value on submit.
setting the selected option in a select element
unable to understand paramValues implicit Object