Java 将HashMap值获取到JSP页面
我正在使用Java 将HashMap值获取到JSP页面,java,twitter-bootstrap,jsp,hashmap,Java,Twitter Bootstrap,Jsp,Hashmap,我正在使用googletranslatorapi翻译客户端上传的文本。我有一个下拉列表来选择语言。但我需要它的代码将其发送到API。因此,我在一个具体的java类中使用了HashMap,如下所示 countryCode.java public class CountryCode { public Map getCountryList() { String fileName = "countryCode.txt"; Map<String, S
googletranslatorapi
翻译客户端上传的文本。我有一个下拉列表来选择语言。但我需要它的代码将其发送到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;
}
}
公共类CountryCode{
公共地图getCountryList()
{
字符串fileName=“countryCode.txt”;
Map code=newhashmap();
弦线;
BufferedReader fileReader=null;
试一试{
fileReader=新的BufferedReader(新的fileReader(文件名));
试一试{
而((line=fileReader.readLine())!=null)
{
字符串[]mapPart=line.split(“:”,2);
如果(mapPart.length>=2)
{
字符串键=mapPart[0];
字符串val=mapPart[1];
代码。put(键,val);
}
}
fileReader.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
for(字符串键:code.keySet())
{
System.out.println(key+”:“+code.get(key));
}
返回码;
}
}
这是我的JSP页面,带有使用引导的下拉列表
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>
我无法理解如何访问此下拉列表中的hashmap值。您有什么想法吗?提前感谢您如果您计划使用scriplets,您可以在使用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']}
//在Servlet中,您可以这样设置属性
Map myCode=myId.getCountryList();
setAttribute(“代码”,myCode);
//然后在Jsp中,您可以通过这种方式访问映射
${code.nameofyourfield}
//或
${code['nameofyourfield']}
调用getCountryList()后如何继续?在标记中设置变量后,可以通过表达式语言在Jsp页面上使用它。不过我不建议使用scriplets。这被认为是不好的做法。查看@Nicolas Filotto的答案:这将是一个很好的选择。你能给我一个关于如何将hashmap值传递给dropbox的示例代码吗?将值传递给dropbox?我的答案是基于你发布的代码。你需要参考你正在使用的任何API。我非常抱歉。我指的是下拉列表。我试过这个。但它给出了一个错误