SpringMvc接收參數,springmvc接收map參數

來源:互聯網
上載者:User

SpringMvc接收參數,springmvc接收map參數
接收參數的方式:1.HttpServletRequest方式接收 public ModelAndView test1(HttpServletRequest req){        String userName = req.getParameter("userName");        String password = req.getParameter("password");        System.out.println(userName);        System.out.println(password);        return new ModelAndView("jsp/hello");    }2.@RequestParam方式  public ModelAndView test2(String userName,            @RequestParam("password") String pwd){        System.out.println(userName+","+pwd);        return new ModelAndView("jsp/hello");    }3.對象的方式接收 public ModelAndView test3(User user){        System.out.println(user);        return new ModelAndView("jsp/hello");    }4.   /**   * 使用ModelAndView傳出參數 內部 HttpServletRequest的Attribute傳遞 到jsp頁面   * ModelAndView(String viewName,Map data)data是處理結果   */@RequestMapping("action")public ModelAndView test4(User user){     Map<String, Object> data = new HashMap<String, Object>();     data.put("user", user);     return new ModelAndView("jsp/hello",data);} 5. Session的方式/**     * session儲存   可以使用HttpServletRequest的getSession方法訪問     */    @RequestMapping("action")    public ModelAndView test7(HttpServletRequest req){        HttpSession session = req.getSession();        session.setAttribute("salary", 6000.0);        return new ModelAndView("jsp/hello");    } 6.重新導向:@RequestMapping("/updateitem")//spirngMvc可以直接接收pojo類型:要求頁面上input框的name屬性名稱必須等於pojo的屬性名稱public ModelAndView updateitem(Items items){ itemsService.updateitems(items); //不可以加斜杠 解析不了 itemList.actionreturn new ModelAndView(new RedirectView("itemList.action"));} 7.重新導向@RequestMapping("/updateitem")//spirngMvc可以直接接收pojo類型:要求頁面上input框的name屬性名稱必須等於pojo的屬性名稱public String updateitem(Items items){ itemsService.updateitems(items);//重新導向到action 可以加斜杠 redirect:/itemList.action 解析的了return "redirect:itemList.action";}   

使用Model和ModelMap的效果一樣,如果直接使用Model,springmvc會執行個體化ModelMap。

如果使用Model則可以不使用ModelAndView對象,Model對象可以向頁面傳遞資料,View對象則可以使用String傳回值替代。不管是Model還是ModelAndView,其本質都是使用Request對象向jsp傳遞資料。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.