我有一个map<String, Set<String>>
。我试图将该对象的值从 servlet 传递到 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);
我的 jsp 中有两个选择输入。我试图使第二个选择的选项( Set<String>
的值)根据第一个选择中选择的选项( key
)进行更改。
<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>
问题是:我是否需要 javascript,或者我可以通过传递和转换请求中发送的对象来完成此操作,例如 Map<String, Set<String>> filter= (HashMap<String, Set<String>>) request.getAttribute("servletName");
,然后迭代它并准备两个选择?
谢谢。
最佳答案
实现目标的最佳方法是
首先迭代 Set 的所有键并构建第一个下拉列表
<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">
将 onChange 事件添加到第一个下拉列表中,每次更改时都会对服务器进行 ajax 调用并检索正确的值并构建第二个下拉列表
<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>
关于javascript - 将 Map<String, Set<String>> 从 Servlet 传递到 JSP Select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42953820/