Solved

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

Posted on 2011-03-09
6
441 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
Independent Software Vendors: 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
There is a huge demand for CodeIgniter among the PHP web developers due to its dynamic features and benefits these days. It is one of most popular and agile open source PHP framework for creating robust web applications in PHP web development field.…
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…

679 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