我正在使用Google translator API来翻译客户端上传的文本。我有一个选择语言的下拉列表。但我需要它的代码把它发送给API。因此,我在一个具体的java类中使用了HashMap,如下所示
countryCode.java
public class CountryCode {
public Map getCountryList()
{
String fileName = "countryCode.txt";
Map<String, String> code = new HashMap<String, String>();
String line;
BufferedReader fileReader = null;
try {
fileReader = new BufferedReader(new FileReader(fileName));
try {
while((line = fileReader.readLine())!= null)
{
String[] mapPart = line.split(":", 2);
if(mapPart.length >= 2)
{
String key = mapPart[0];
String val = mapPart[1];
code.put(key, val);
}
}
fileReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (String key : code.keySet())
{
System.out.println(key + ":" + code.get(key));
}
return code;
}
}这是我的JSP页面,其中包含使用Bootstrap的下拉列表
translation.jsp
<section class="bg-light-gray">
<div class="container">
<form action="TextTranslation" method="post" class="form" role="form" enctype="multipart/form-data">
<div class="row">
<div id = "imageView" class="col-lg-8 center-block ">
<div class="btn-group">
<a class="btn btn-default dropdown-toggle btn-select2" data-toggle="dropdown" href="#">Select a Region <span class="caret"></span></a>
<ul class="dropdown-menu">
<%
%>
</ul>
</div>我不能理解如何访问这个dropdown.Do中的hashmap值你有什么想法吗?
发布于 2016-04-05 15:58:59
如果您计划使用脚本,则可以在使用jsp标记之后使用纯Java
<jsp:useBean id="myId" class="package.name.CountryCode"/>
<%
Map<String, String> myCode = myId.getCountryList();
// do something with myCode
%>如果通过servlet将地图传递给Jsp,则可以简单地将地图存储为属性,然后通过表达式语言访问它
// within the Servlet you could set the attribute this way
Map<String, String> myCode = myId.getCountryList();
request.setAttribute("code", myCode);
// then within the Jsp you would access the map this way
${code.nameofyourfield}
// or
${code['nameofyourfield']}https://stackoverflow.com/questions/36419532
复制相似问题