Solved

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

Posted on 2011-03-09
6
437 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
  • 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now