?
Solved

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

Posted on 2011-03-09
6
Medium Priority
?
448 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 400 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 28

Accepted Solution

by:
rrz earned 1600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 28

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 28

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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)

719 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