Solved

Retaining the value of a select during and after a Struts action

Posted on 2011-03-09
6
444 Views
Last Modified: 2012-05-11
How does one "hold on" to a selected value on a web page or JSP after the page reloads on completion of a submitted Struts action?

Thanks.
0
Comment
Question by:jwright9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 18

Assisted Solution

by:Jerry Miller
Jerry Miller earned 100 total points
ID: 35090882
I sometimes use a hidden field to store values to be used later in a process.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx

Another option I use is to enable view state.

http://msdn.microsoft.com/en-us/library/ms972976.aspx
0
 

Author Comment

by:jwright9
ID: 35123498
Hi jmiller, I am working in Java and am experiencing this problem in a JSP.  The hidden variable technique I tried.  It should have worked don't know what the problem is.
0
 
LVL 27

Accepted Solution

by:
rrz earned 400 total points
ID: 35124035
I don't use Struts. So, I can't tell you about how this is done the Struts way.
But I can show you how JSP and Javascript can work together to accomplish what you want to do. Please copy and paste this demonstration JSP.  It is just a simple JSP that sends its request to itself.  
<%
   String selectedValue = request.getParameter("selectNumber");
   if(selectedValue == null)selectedValue = "Please select a number";
%>
<html>
<head>
<script>
         function retainValue(){
              var element = document.forms[0].selectNumber;
              for(x = 0; x < element.length; x++){
                      if(element.options[x].value == "<%=selectedValue%>"){
                            element.options[x].selected = true;
                            break;
                      } 
              }
         }
</script>
</head>
<body onload="retainValue();">
<form>
<select name="selectNumber" >
            <option value="Please select a number">Please select a number</option>
           <option value="one">one</option>
           <option value="two">two</option>
           <option value="three">three</option> 
           <option value="four">four</option> 
</select>
<input type="submit"/>
</form>
</body>
</html>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:rrz
ID: 35124454
I saw this,
http://www.javalobby.org/articles/struts/ 
So I guess in Struts you populate your form bean and then get the value from it instead of using a scriptlet as I have posted.
0
 
LVL 27

Expert Comment

by:rrz
ID: 35124476
I suggest that you ask a question about this topic at  
http://www.experts-exchange.com/Programming/Languages/Java/   
You might be able to get a Struts expert to help you.
0
 

Author Closing Comment

by:jwright9
ID: 35142593
I really liked rrz's code example.  He or she really put effort into answering this for me.  Thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to implement server side field validation and display customized error messages to the client.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question