2012-05-10 33 views
2

我想送3個PARAMS控制器具有以下:HREF多參數春天

<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

你的代碼是確定的,但是這是Spring路:

@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:/"; 
    } 
} 

此外,如果param未設置爲on,則可以使用@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:/"; 
}