4

Out of the many solutions I've found, I can't seem to get any of them to work for me. I've got a dropdown list in my jsp file:

    <select name="chosenOne" onchange="javascript:getUsers(this.value);">  
            <option value="0" onclick="javascript:getUsers(this.value);">All Modules</option>
            <c:forEach items="${modules}" var="module"> 
                <option value="${module.id}"><c:out value="${module.title}"/></option>  
            </c:forEach>  
        </select></p> 

It populates dynamically from my database, except for the "All Modules" option. Here's my javascript function for the onchange event:

       <script type="text/javascript">
        function getUsers(id) { 
            if (id != "0"){
            document.updateForm.id.value = id;
            }
            else{
            document.updateForm.id.value = "0";
            }
            document.updateForm.submit(); 
        }</script>

And here's my servlet code that deals with the dropdown box amongst other things:

        protected void process(final HttpServletRequest request, final HttpServletResponse response)
        throws ServletException, IOException {

    long modID = 0;
    String url = "jsp/user/administration.jsp";
    request.setAttribute("modules", dataAccessor.getAllModules());

    if (isParameterValid(request.getParameter("id"))) {
        modID = Long.parseLong(request.getParameter("id"));
        request.setAttribute("users", getUsersFromModule(modID));
        System.out.println(modID);

    } else if (!isParameterValid(request.getParameter("id"))) {
        request.setAttribute("users", dataAccessor.getAllUsers());

    } else {
        request.setAttribute("errorMessage", "There was a problem retrieving users!");
        url = "jsp/error.jsp";
    }

    //request.setAttribute("formerSelect", modID);
    request.getRequestDispatcher(url).forward(request, response);
}

So how can I get the selected dropdown value to remain in the dropdown box after the form refreshes? I've fiddled around with setting an attribute "formerSelect" which just contains the value of the previously selected item in the dropdown. But then for some reason it rendered my dropdown useless when I tried to assign it to the "selected" value within my options tag. Any help is much appreciated.

2 Answers2

5

You need to pass this parameter to the httpRequest after submitting:

request.setAttribute("selectedModule", request.getParameter("chosenOne"));

and after that you need to mark an option as selected:

<c:forEach items="${modules}" var="module"> 
     <option value="${module.id}" ${module.id == selectedModule ? 'selected':''}>...</option>  
</c:forEach> 
Pianov
  • 1,533
  • 9
  • 16
  • It doesn't seem to work. It still goes back to "All Modules" after form refresh. –  Dec 12 '14 at 17:24
  • 2
    It must work. I've made simple example, you can download here: https://www.dropbox.com/s/o6d2xeidz2xyhbr/select_pass_param.zip?dl=0 – Pianov Dec 12 '14 at 20:38
  • I must be placing the code in the wrong place then. Does the request.setAttribute... go within my servlet class? "${module.id == selectedModule ? 'selected':''}" <----This looks like voodoo to me. Could you explain what this does? Thanks –  Dec 12 '14 at 20:48
  • 1
    You choose some option and submit the form, after that you have all these parameters in your servlet. So, if you need show this form again, you need to pass all populated parameters there. One of them is selectedModule. For restore 'html select' state, you need add 'selected' attribute to 'html option' where one of the module ID equals to selectedModule. – Pianov Dec 12 '14 at 21:05
  • 1
    Some documentation for using EL you can read here: http://docs.oracle.com/javaee/5/tutorial/doc/bnahq.html – Pianov Dec 12 '14 at 21:07
  • Gotcha. So then the "request.setAttribute("selectedModule", request.getParameter("chosenOne"));" would go within one of my servlet functions? –  Dec 12 '14 at 21:32
  • 1
    Exactly, you can see it in my example here: https://www.dropbox.com/s/o6d2xeidz2xyhbr/select_pass_param.zip?dl=0 – Pianov Dec 12 '14 at 21:38
2

I can explain like this. As a example code put this code in your Servlet

String status = request.getParameter("status");
request.setAttribute("status", status);

put this code in jsp file

<select  id="status" name="status" class="listBx"  onChange = "check(this);">
    <option value="" >--- Select ---</option>                           
    <option value="1"  <%if((request.getAttribute("status") != null) && request.getAttribute("status").equals("1")){ %> selected <%} %>>Logged in</option>
    <option value="0"  <%if((request.getAttribute("status") != null) && request.getAttribute("status").equals("0")){ %> selected <%} %>>Logged Out</option>
</select>
Madhuka Dilhan
  • 1,396
  • 1
  • 14
  • 21