Solved

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

Posted on 2011-03-09
6
438 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
New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

 
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

Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSS Scroll Issue 3 90
CSS Position not returning 2 48
how to add new optional parameter to JSP 1 29
Message not shown 5 33
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

920 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

12 Experts available now in Live!

Get 1:1 Help Now