[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 565
  • Last Modified:

Retain the values in dropdown

I have a drop down of state list and needs to remeber the selected value once page reloaded. it is in JSP and Struts .
0
mgoutham22
Asked:
mgoutham22
  • 3
  • 2
  • 2
1 Solution
 
JavatmCommented:
make hidden textfield then on page reload pass the value to the hidden textfield in that way you retain the value from the list.
0
 
JavatmCommented:
Here's a sweet demo (by default the result is null until you select a value and submit) :

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
    <h1>Retain Demo</h1><br>
    <form method="post" action="demo.jsp">
    <select name="List" class="style5" style="border:thin; background:#6AB5FF; color:#FFFFFF">
                      <option value="CheeseBurger" selected>CheeseBurger</option>
                      <option value="HamBurger">HamBurger</option>
                      <option value="DoubleCheeseBurger">DoubleCheeseBurger</option>
    </select>
    <% String user1 = request.getParameter("List"); %>
    <br><br><input type="hidden" name="hdf1" value="<%=user1%>">
    Value of hidden field is : <% out.println("" + user1); %>
    <br><br><input type="submit" value="submit">
    </form>    
    </body>
</html>
0
 
fargoCommented:
One other way is to keep the action form in session scope and it will then retain the last state [this will avoid any hidden field handling]
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!

 
fargoCommented:
In struts-config.xml, for a particular action

<action
       path="/showPage"
       type="com..xx.whateverAction"
       scope="session"
       name="pageForm"
       validate="false">
         <forward
          name="success"
          path="/pages/whateverPage.jsp" />
    </action>

scope is defined to session here.
0
 
mgoutham22Author Commented:
I am sorry but that did not work for me . any other tips , i clicked on accept with out testing .

0
 
fargoCommented:
do u have a reset method in the actionForm? if yes, remove it. You need to restart the tomcat after the struts-config change. You can very well reopen the question.
0
 
mgoutham22Author Commented:
no reset method , i did restart weblogic after change.
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.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now