首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将JSP值获取到HashMap页面

问将JSP值获取到HashMap页面
EN

Stack Overflow用户
提问于 2016-04-05 15:14:31
回答 1查看 1.4K关注 0票数 0

我正在使用Google translator API来翻译客户端上传的文本。我有一个选择语言的下拉列表。但我需要它的代码把它发送给API。因此,我在一个具体的java类中使用了HashMap,如下所示

countryCode.java

代码语言:javascript
复制
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

代码语言:javascript
复制
<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值你有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2016-04-05 15:58:59

如果您计划使用脚本,则可以在使用jsp标记之后使用纯Java

代码语言:javascript
复制
<jsp:useBean id="myId" class="package.name.CountryCode"/>

<% 
    Map<String, String> myCode = myId.getCountryList();
    // do something with myCode
%>

如果通过servlet将地图传递给Jsp,则可以简单地将地图存储为属性,然后通过表达式语言访问它

代码语言:javascript
复制
// 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']}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36419532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档