2016-12-19 110 views

答えて

2

html select要素にname属性を渡すと、以下のようにHttpServletRequestオブジェクトからコントローラのドロップダウンの選択値にアクセスできます。

<form role="form" id="sendAddress" th:[email protected]{/sendAddress} method="post"> 
     <select class="form-control" name="nameOfCity"> 
      <option value="">Select City</option> 
      <option value="HYD">Hyderabad</option> 
      <option value="MUM">Mumbai</option> 
      <option value="DEL">Delhi</option> 
     </select> 
    </form> 



    @RequestMapping(value={"/sendAddress"},method = RequestMethod.POST) 
    public String messageCenterHome(Model model,HttpSession session,HttpServletRequest request) { 

     String selectedCity= request.getParameter("nameOfCity") 
     //return view 
    } 
+0

私は、これは適切なTheHymeleafの解決策ではないと思うが、それは私のために働く。ありがとう。 – Johannes

0

Java MVCフレームワークはServlet APIをカプセル化し、フレームワークのより単純なAPIを使用して共通の機能を提供します。

リクエストからパラメータ値を取得するのと同じようなタスクでは、Spring MVCとBootは、@RequestParamアノテーションを使用して、selectタグ付きオプションタグから選択されたパラメータ値を取得します。したがって、ak38のコードでは、パラメータを取得するのに、HttpServletRequestをまったく使用する必要はありません。あなたの避難所ならば、それを試してみてください

@RequestMapping(value={"/sendAddress"}, method = RequestMethod.POST) 
public String messageCenterHome(**@RequestParam** String *nameOfCity*) { 
    // value of nameOfCity is now value of "nameOfCity" paramter, 
    // that is, the value of the option tag selected 
    // return view 
} 

HTMLフォーム

<form role="form" id="sendAddress" th:[email protected]{/sendAddress} method="post"> 
    <select class="form-control" name="*nameOfCity*"> 
     <option value="">Select City</option> 
     <option value="HYD">Hyderabad</option> 
     <option value="MUM">Mumbai</option> 
     <option value="DEL">Delhi</option> 
    </select> 
</form> 

コントローラ代わりに、それだけで、要求の呼び出しコントローラメソッドのPARAMTERに注釈を付けます私はちょうどこれを見たことがあり、質問が投稿されてから数ヶ月が経ちました。

関連する問題