• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

JAVA:SpringMvc接收参数方法汇总

java 搞代码 4年前 (2022-01-09) 26次浏览 已收录 0个评论

下面小编就为大家带来一篇SpringMvc接收参数方法总结(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

接收参数的方式:

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();    sessio<p>本文来源gao!daima.com搞$代!码#网#</p>n.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传递数据。

以上就是JAVA:SpringMvc接收参数方法汇总的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:JAVA:SpringMvc接收参数方法汇总

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址