2012-05-10 4 views
2

春:hrefが、私は次のようにコントローラに3つのparamsを送信したい

<li><a href="result?name1=thor&name2=hulk&name3=loki">SMTH</a></li> 

コントローラ:

@Controller 
@RequestMapping(value="/result") 
public class SmthController { 

    @RequestMapping(method=RequestMethod.GET) 
    public String dosmth(HttpServletRequest request) { 

     String one = request.getParameter("name1"); 
     String two = request.getParameter("name2"); 
     String three = request.getParameter("name3"); 


     JOptionPane.showMessageDialog(null, 
       " Param 1 is:" +one +" \n Param 2 is: " +two +" \n Param 3 is: " +three); 

     return "redirect:/"; 
    } 
} 
+4

をそして、あなたの質問はありますか? –

答えて

2

あなたのコードはOKですが、これは春の方法です:PARAMがオンに設定されていない場合

@Controller 
@RequestMapping(value="/result") 
public class SmthController { 

    @RequestMapping(method=RequestMethod.GET) 
    public String dosmth(HttpServletRequest request, @RequestParam("name1") String one, @RequestParam("name2") String two, @RequestParam("name3") String three) { 

     JOptionPane.showMessageDialog(null, 
       " Param 1 is:" +one +" \n Param 2 is: " +two +" \n Param 3 is: " +three); 

     return "redirect:/"; 
    } 
} 

また、デフォルト値を確立するため@RequestParamのdefaultValue属性を使用することができますGETリクエスト。

0

私は地図使ってメソッドのシグネチャを簡素化することを好む:

@RequestMapping(method=RequestMethod.GET) 
public String dosmth(@RequestParam Map<String, String> map) { 
    System.out.println("Param 1 is:" + map.get("paramName1") + "Param 2 is:" + map.get("paramName2")); 
    return "redirect:/"; 
} 
関連する問題