2016-04-06 8 views
0

私は言語名からなるドロップダウンを持っています。私は値を設定し、ハッシュマップを使ってドロップダウンの名前を表示しています。Javaを使用して選択したドロップダウンアイテムにアクセスする

<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属性は特定のサーブレットでアクセスされます。しかし、それはヌル値を与えます。あなたが任意のアイデアを持っていますか?同様に

request.getParameter("code"); 
+0

を実装ソリューション上記の場合は、あなたのjsp-codeから削除あなたの設定値(別名、 "code" - > name)がnullの権利として取得されていますか? –

+0

私はrequest.getParameter( "code")を使用します。私はこの値を印刷するときにnull値を得ます –

答えて

0

更新あなたのJSP、

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

そして、私が使用したサーブレットで

<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%>/> . 

UPDATED事前

にありがとう同じようにあなたのサーブレットからそれを得る、

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

注:

あなたは、サーブレット側で、その後(TextTranslation)サーブレットでこのフォームを送信しようとしている間、あなたがつもり、その後、

request.setAttribute("code", name); 
+0

それでもnull値を与えます –

+0

@TharinduRanasingheサーブレットのフォームに直接送信していますか? –

+0

はい。これは更新されたコードです。 サーブレットでは、request.getParameter( "code");を使用しました。 –

関連する問題