2017-03-22 58 views
0

我有一个map<String, Set<String>>。我试图将这个对象的值从servlet传递给jsp。传递Map <String,将<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");,然后遍历它并准备两个选择?

谢谢。

+0

在你有你的代码,它不可能使你的目标的方式,因为第一迭代所有的键,然后按键建立下拉...也许你不得不再次重复所有键和每个键解压集和建立具体的下拉菜单 – cralfaro

+0

@cralfaro我会尝试你的建议;问题是我不知道哪个键被选中以便改变值。而且我不受限于某种方式,如果您有不同的方式来使其工作,我可以更改代码。 – M20

+0

其实我会选择不同的方法,因为你需要第一个下拉菜单,第二个是根据第一个下拉菜单的值加载的,我会在第一个下拉菜单中添加一个onChange事件,然后对服务器进行ajax调用为第二个下拉菜单加载正确的值,您的代码将变得更清晰,更容易维护。 – cralfaro

回答

1

的最好方法在你设定的所有关键让你的目标,其

  1. 首先迭代,并建立第一个下拉

    <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"> 
    
  2. 的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>