英文:
How to use treemap in java script
问题
以下是您要翻译的内容:
"我从Java Servlet发送了一个树状图到JSP。我能够在JSP页面内使用树状图的键作为下拉列表。我的地图包含国家名称作为键,首都作为值,当用户选择国家名称时,我只需要在文本框中显示国家首都。我需要在表单提交后在Java页面中获取所选的键值(国家名称),我知道如何做这个。当我从JavaScript中读取地图时,它被识别为字符串/[object Object]。我如何在这里使用map.get(key)。提前感谢您。
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>
<details>
<summary>英文:</summary>
I am sending one treemap from java servlet to jsp. I am able to use the treemap keys as drop down list inside jsp page . 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]. how can i use map.get(key) here. 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 >
</details>
# 答案1
**得分**: 0
Finally, I found the answer myself. I have converted treemap to json object in the servlet class and pushed to jsp.
Servlet Java:
```java
Gson gson = new Gson();
String json = gson.toJson(countrylist);
request.setAttribute("json", json);
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. It works fine for me, not sure if it is the right way.
英文:
Finally, I found the answer myself.I have converted treemap to json object in the servlet class and pushed to jsp
Servelt java
Gson gson = new Gson();
String json=gson.toJson(countrylist);
request.setAttribute("json", json);
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. It works fine for me, not sure it is a right way.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论