简体   繁体   English

如何在 java 脚本中使用树形图

[英]How to use treemap in java script

I am sending one treemap from java servlet to jsp.我正在从 java servlet 向 jsp 发送一个树形图。 I am able to use the treemap keys as drop down list inside jsp page.我可以在 jsp 页面中使用树图键作为下拉列表。 My map contains country name as key and capital as value when user selected country name i just need to display country capital in the text box.I need selected key value(country name) in the java page after form submission(I know hot to do this).when i am reading map from java script it is taking as string/[object Object].我的 map 包含国家名称作为键和大写作为值当用户选择国家名称时我只需要在文本框中显示国家资本。我需要在 java 页面中选择键值(国家名称)提交表单后(我知道热门要做这)。当我从 java 脚本中读取 map 时,它被视为字符串/[对象对象]。 how can i use map.get(key) here.我如何在这里使用 map.get(key)。 Thanks in advance提前致谢

function fillText(obj)
    {
               
               var key= obj.options[obj.selectedIndex].value;
           var map = new Map('<%=request.getAttribute("countrylist")%>');
           
           var contract = "${countrylist}";
           
           
    }
    <label for="Select The Switch">Select The Switch:</label>
           <select name="country" id="countrycode"  onchange="fillText(this)" required>
           <option value= "" >Please select the switch</option>
        <c:forEach items="${countrylist}" var="countrylist">
        <option value=${countrylist.key}>${countrylist.key}</option>
        </c:forEach>
       
            </select>`)  
    Country capital:<input type="text" id="pass" readonly >

Finally, I found the answer myself.I have converted treemap to json object in the servlet class and pushed to jsp最后,我自己找到了答案。我已经在servlet class 中将treemap 转换为json object 并推送到ZEC407CCE157B64973976E2

Servelt java塞尔维特 java

Gson gson = new Gson();
String json=gson.toJson(countrylist);
request.setAttribute("json", json);

javascript function javascript function

function fillText(obj)
{
           
                  var key= obj.options[obj.selectedIndex].value;
                      var json=${json};
                  var value=json[key]
                  document.getElementById("pass").value=value;
                  
}

import gson-2.8.0.jar to the class path.将 gson-2.8.0.jar 导入 class 路径。 It works fine for me, not sure it is a right way.它对我来说很好,不确定它是否是正确的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM