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 Show the selected value in dropdown again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Show the selected value in dropdown again" Watch "Show the selected value in dropdown again" New topic
Author

Show the selected value in dropdown again

thomas colding
Ranch Hand

Joined: Mar 23, 2007
Posts: 60
I have a JSP Page with a drop down select boxes that is used to make a search.

My challenge is to show the same value as selected in the Dropdown when the search result is returned.


My Dropdown is builds as following on my JSP Page

<SELECT name="montno" onChange="selectChanged(this);">
<OPTION value="1" selected>1</OPTION>
<OPTION value=2>2</OPTION>
<OPTION value=3>3</OPTION>
<OPTION value=4>4</OPTION>
<OPTION value=5>5</OPTION>
<OPTION value=6>6</OPTION>
<OPTION value=7>7</OPTION>
</SELECT>




In procesaction I retrieve the selected values from the hidden field I have added the selected values to.

String sSearchString = request.getParameter("numberoffmonth");


Then I Add it to the response to retrieve it in Process view

response.setRenderParameter("montno",sSearchString);



What do I have to do so it is selected value when the JSP is visible again.?

Thanks
manoj r patil
Ranch Hand

Joined: Jun 06, 2002
Posts: 181
I think you can set the "selected" in the following code dynamically while returning the dropdown list?



...or am I missing something?


love your job and not your company;
...because you never know when your company will stop loving you!
thomas colding
Ranch Hand

Joined: Mar 23, 2007
Posts: 60
mannoj patil wrote:I think you can set the "selected" in the following code dynamically while returning the dropdown list?



...or am I missing something?



What do you mean , if you see in the above html code, it is alreadey selected.
Bu my challenge is how to show value the User has selected afte the Search result is returned.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61196
    
  66

Same way. Whatever option has the selected attribute will be the selected option.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
thomas colding
Ranch Hand

Joined: Mar 23, 2007
Posts: 60
Bear Bibeault wrote:Same way. Whatever option has the selected attribute will be the selected option.


Could you please specify what you mean, is it possible?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

thomas colding wrote:

response.setRenderParameter("montno",sSearchString);



why?
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

What is the purpose of following function

I could not find this function as a part of javax.servlet.http.HttpServletResponse in j2ee 5.p api library


Regards
Salil Verma
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61196
    
  66

thomas colding wrote:
Bear Bibeault wrote:Same way. Whatever option has the selected attribute will be the selected option.


Could you please specify what you mean, is it possible?

I'm not sure how I could possibly make it more clear.

Whatever option you put the selected attribute on will be the selected option. This is just basic HTML.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

Bear Bibeault wrote:This is just basic HTML.


That's the key thing to understand. To answer your question there are two steps:

(1) Figure out what HTML you want the browser to get.

(2) Write your JSP to produce that HTML.

You don't seem to have done step (1) properly. And then if you don't know what HTML you want to produce, it's hard to write code to produce it.
thomas colding
Ranch Hand

Joined: Mar 23, 2007
Posts: 60
It sound after all the input that I have to built the Dropdown list dynamically and set the value that the user has selected at default
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61196
    
  66

Bingo.
thomas colding
Ranch Hand

Joined: Mar 23, 2007
Posts: 60
Bear Bibeault wrote:Bingo.

Sorry but as I mentioned I am Newbie and not used to develop Webapplications.
suchit pandya
Ranch Hand

Joined: Oct 27, 2009
Posts: 43
if you can use java script on page then you can do below code.

also store selected value in request and after when page loads back retrive both the values.

thomas colding
Ranch Hand

Joined: Mar 23, 2007
Posts: 60
Thanks for the input.
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey suchit,

Implementing this in jsp using el or scriptlet would be better option than implementing this in java script due to following to reasons -
1- If java script is disabled in browser, this functionality would not work
2-it would require some additional data to be sent to the browser resulting in performance hit.

thomas colding
Ranch Hand

Joined: Mar 23, 2007
Posts: 60
Hi

Would it be better approach to build the dropdown content in java process action, and then transfer it to the JSP-page.
A dynamical list.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61196
    
  66

No. Just use JSP conditional mechanisms to set the selected attribute on the right option. This is done all the time.

What part are you having trouble wrapping your mind around? What the HTML should look like, or how to write JSP expressions to create that HTML?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

From what I can see, at least from the original post, Thomas already has JSP code which generates the SELECT element. All that's needed is to improve that to add the "selected" attribute on the appropriate OPTION element. Perhaps the question is how to do that; I can see that being a FAQ. I know I had trouble figuring out a way to do that in the EL.
thomas colding
Ranch Hand

Joined: Mar 23, 2007
Posts: 60
Paul Clapham wrote:From what I can see, at least from the original post, Thomas already has JSP code which generates the SELECT element. All that's needed is to improve that to add the "selected" attribute on the appropriate OPTION element. Perhaps the question is how to do that; I can see that being a FAQ. I know I had trouble figuring out a way to do that in the EL.



Exactly what I had in the mind, they must be some off you that may have create a Drowdown in which you select a value. submit the form.
And when they JSP is visible again with a result, the last selected value should be the one should as default selected in the dropdown.
So at least it is possible to what the result was based on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Show the selected value in dropdown again