使用Java访问选定的下拉项
我有一个由语言名称组成的下拉列表。我使用hashmap设置下拉列表的值并显示其名称使用Java访问选定的下拉项,java,jsp,html-select,Java,Jsp,Html Select,我有一个由语言名称组成的下拉列表。我使用hashmap设置下拉列表的值并显示其名称 <form action="TextTranslation" method="post" class="form" role="form" > <div class="row"> <div id = "imageView" class="col-lg-8 center-block "> <div class=
<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>
同样地更新您的jsp
<form...>
...
<input type="hidden" name="code" value = <%= name%>/>
....
</form>
request.getParameter("code"); // will return value of code
注意:
从您的jsp代码中删除,如果您要实现上述解决方案
request.setAttribute("code", name);
当您准备在Servlet(TextTranslation)上提交此表单时,在Servlet端,您得到的设置值(aka,“code”->name)为空,对吗?我使用request.getParameter(“code”);当我打印这个值时,我得到一个空值,它仍然给出空值value@TharinduRanasinghe您正在向servlet提交表单,对吗?是的。这是更新后的代码@Tharindurasinghe看到我在你的问题中更新了你的“更新代码”,在里面我可以看到选项标签没有正确关闭。你能做到吗。如果仍然没有解决,请让我进一步了解。@Tharindurasingh将隐藏输入标记的值用双引号括起来。
request.getParameter("code"); // will return value of code
request.setAttribute("code", name);