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  回復  更多評論   


          主站蜘蛛池模板: 太仓市| 固原市| 长岭县| 灵宝市| 南乐县| 临江市| 永州市| 法库县| 济阳县| 迭部县| 英山县| 张家川| 南宫市| 溧水县| 沈阳市| 崇礼县| 丰城市| 和顺县| 邯郸县| 洛南县| 海晏县| 东乡县| 山东省| 柏乡县| 库车县| 新安县| 丰县| 拉萨市| 定州市| 仙桃市| 屏东市| 龙岩市| 湄潭县| 晋州市| 岳西县| 文登市| 和顺县| 繁昌县| 铜陵市| 乐清市| 如皋市|