[英]Passing Map<String, Set<String>> from Servlet to JSP Select
我有一個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");
,然后遍歷它並准備兩個選擇?
謝謝。
達成目標的最佳方法
首先遍歷集合的所有鍵並構建第一個下拉列表
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.