Retain the values in dropdown

Posted on 2006-04-06
Last Modified: 2008-03-10
I have a drop down of state list and needs to remeber the selected value once page reloaded. it is in JSP and Struts .
Question by:mgoutham22
    LVL 15

    Expert Comment

    make hidden textfield then on page reload pass the value to the hidden textfield in that way you retain the value from the list.
    LVL 15

    Expert Comment

    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"

            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        <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>
        <% 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">
    LVL 11

    Expert Comment

    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]
    LVL 11

    Accepted Solution

    In struts-config.xml, for a particular action

              path="/pages/whateverPage.jsp" />

    scope is defined to session here.

    Author Comment

    I am sorry but that did not work for me . any other tips , i clicked on accept with out testing .

    LVL 11

    Expert Comment

    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.

    Author Comment

    no reset method , i did restart weblogic after change.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Title # Comments Views Activity
    Jquery ajax file upload not working 11 125
    Set up Wildfly 10 8 277
    TreeSet comparator example 7 86
    countXY challenge 28 90
    PRTG Network Monitor lets you monitor your bandwidth usage, so you know who is using up your bandwidth, and what they're using it for.
    ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    779 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

    14 Experts available now in Live!

    Get 1:1 Help Now