Issue
I have a map<String, Set<String>>
. I trying to pass the values for this object from servlet to jsp.
Map<String, Set<String>> filter_info= new HashMap<String, Set<String>>();
for(String key: results.keySet())
{
filter_info.put(key.trim(), new HashSet<String>(Arrays.asList(results.get(key).split(","))));
}
request.setAttribute("filter",filter_info);
this.getServletContext().getRequestDispatcher("/Search.jsp")
.forward(request, response);
I have two select inputs in my jsp. I am trying to make the options (values of Set<String>
) of the second select to change depending on the option (key
) selected in the first select.
<form role="search" class="search-form" id="search-form" action="#" method="post">
<label for="Keys">Keys: </label>
<select id="Keys">
<c:forEach var="filter" items="${filter}">
<option value="${filter.key}">
<c:out value="${filter.key}"/>
</option>
</c:forEach>
</select>
<br/>
<label for="SetValues">Values: </label>
<select id="SetValues">
//the part I don't know how to change according to the key selected
</select>
<input type="submit" value="Filter" class="search-button">
</form>
The question is: do I need javascript or can I do it whith passing and casting the object sent in the request, for example Map<String, Set<String>> filter= (HashMap<String, Set<String>>) request.getAttribute("servletName");
, then iterate through it and prepare the two selects ?
Thanks.
Solution
The best approach to get your goal its
First iterate over all key of your Set and build the first dropdown
<label for="Keys">Keys: </label> <select id="Keys" onchange="updateByKey()"> <c:forEach var="filter" items="${filter}"> <option value="${filter.key}"> <c:out value="${filter.key}"/> </option> </c:forEach> </select> <br/> <input type="submit" value="Filter" class="search-button">
Add an onChange event to the first dropdown and with every change make an ajax call to the server and retrieve the right values and build the second dropdown
<label for="SetValues">Values: </label> //here load the dropdown with all options of the selected key in the previous dropdown <select id="SetValues"> //iterate all options of this specific key </select>
Answered By - cralfaro
Answer Checked By - Pedro (JavaFixing Volunteer)