paulwong

          Spring MVC 的請求參數獲取的幾種方法

          通過@PathVariabl注解獲取路徑中傳遞參數

          JAVA
           @RequestMapping(value = "/{id}/{str}")
           
          public ModelAndView helloWorld(@PathVariable String id, @PathVariable String str) {
           System.out.println(id);
           System.out.println(str);
           
          return new ModelAndView("/helloWorld");
          }


          用@ModelAttribute注解獲取POST請求的FORM表單數據
          JSP
          <form method="post" action="hao.do">
           a: 
          <input id="a" type="text" name="a"/>
           b: 
          <input id="b" type="text" name="b"/>
           
          <input type="submit" value="Submit" />
          </form>


          JAVA pojo
           public class Pojo{
           
          private String a;
           
          private int b;
          }


          JAVA controller
          @RequestMapping(method = RequestMethod.POST)
          public String processSubmit(@ModelAttribute("pojo") Pojo pojo) {
           
          return "helloWorld";
           }


          直接用HttpServletRequest獲取
          JAVA
          @RequestMapping(method = RequestMethod.GET)
          public String get(HttpServletRequest request, HttpServletResponse response) {
           System.out.println(request.getParameter(
          "a"));
           
          return "helloWorld";
          }


          用注解@RequestParam綁定請求參數a到變量a
          當請求參數a不存在時會有異常發生,可以通過設置屬性required=false解決,
          例如: @RequestParam(value="a", required=false)
          JAVA
          @RequestMapping(value = "/requestParam", method = RequestMethod.GET)
          public String setupForm(@RequestParam("a") String a, ModelMap model) {
           System.out.println(a);
          return "helloWorld";}

          posted on 2012-03-23 17:36 paulwong 閱讀(51169) 評論(2)  編輯  收藏 所屬分類: SPRING MVC

          Feedback

          # re: Spring MVC 的請求參數獲取的幾種方法 2014-04-03 11:11 2

          32  回復  更多評論   

          # re: Spring MVC 的請求參數獲取的幾種方法 2014-05-11 14:30 zuidaima

          可以參考代碼:springmvc請求接收參數的幾種方法 代碼下載地址:http://www.zuidaima.com/share/1751862044773376.htm  回復  更多評論   


          主站蜘蛛池模板: 河北省| 延安市| 二连浩特市| 筠连县| 乐昌市| 武平县| 五家渠市| 色达县| 元朗区| 旺苍县| 定襄县| 金门县| 蒙自县| 耿马| 金湖县| 仙游县| 吉安县| 陆河县| 浦县| 永兴县| 荆门市| 婺源县| 宁晋县| 静海县| 加查县| 九龙县| 宜黄县| 皋兰县| 义马市| 绥滨县| 闽清县| 新竹市| 剑阁县| 鄂托克前旗| 馆陶县| 博爱县| 河东区| 罗山县| 丁青县| 大埔区| 滦平县|