使用Java访问选定的下拉项 [英] Accessing selected dropdown items using Java

查看:106
本文介绍了使用Java访问选定的下拉项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含语言名称的下拉菜单.我正在使用哈希图设置值并显示下拉列表的名称.

I have a dropdown which consist the language names. I am setting the value and displaying name of the dropdown by using a hashmap.

 <form action="TextTranslation" method="post" class="form" role="form" >
        <div class="row">
            <div  id = "imageView" class="col-lg-8 center-block ">

         <div class="btn-group"> 
         <select name="country">

            <% 

          Map<String,String> langCode = x.getCountryList();
           for( Object key :langCode.keySet() )
           {%>


           <option value="<%=(String)key%>"><%=langCode.get(key) %> </option>
              <% 

              System.out.println((String)key);
           }

           String name =  request.getParameter("country");
           request.setAttribute("code", name);

            %>



        </select>
    </div>
        <input type="submit" class= "btn btn-image" value="Translate">

                <a href="#search" class="btn btn-default bg-light-gray">Search Text</a>

            </div>
        </div>
          </form>

值会正确地传递到保管箱,因为它会在控制台中打印所有值. set属性是在特定的servlet中访问的.但是它给出一个空值.你有什么主意吗?谢谢

Values are passed correctly to dropbox as it print all the values in console. the set attribute is accessed in the particular servlet. But it gives a null value. Do you have any idea?Thank you in advance

已更新

<select name="country"> 
    <% 
       Map<String,String> langCode = x.getCountryList(); 
        for( Object key :langCode.keySet() ) 
          {%>
               <option value="<%=(String)key%>"><%=langCode.get(key) %> /option> 
               <% System.out.println((String)key); 
           } 
         String name = request.getParameter("country"); 
    %> 
</select> 

<input type="hidden" name="code" value = <%= name%>/> .

在我使用的servlet中,

In the servlet I used,

 request.getParameter("code");

推荐答案

同样更新您的jsp,

<form...>
...
<input type="hidden" name="code" value = <%= name%>/> 
....
</form>

然后同样从servlet获取它,

then get it from your servlet likewise,

 request.getParameter("code"); // will return value of code

注意:

如果要实施上述解决方案,则从jsp-code中删除

Remove from your jsp-code if above solution you gonna implement then,

 request.setAttribute("code", name);

这篇关于使用Java访问选定的下拉项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆