spring 前台界面和control的交互 modelAndView

接收参数的方式:
1.HttpServletRequest方式接收
?
1
2
3
4
5
6
7
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方式
?
1
2
3
4
5
public ModelAndView test2(String userName,
@RequestParam("password") String pwd){
System.out.println(userName+","+pwd);
return new ModelAndView("jsp/hello");
}
3.对象的方式接收
?
1
2
3
4
public ModelAndView test3(User user){
System.out.println(user);
return new ModelAndView("jsp/hello");
}
4.
?
1
2
3
4
5
6
7
8
9
10
/**
* 使用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的方式
?
1
2
3
4
5
6
7
8
9
/**
* 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.重定向:
?
1
2
3
4
5
6
7
8
9
@RequestMapping("/updateitem")
//spirngMvc可以直接接收pojo类型:要求页面上input框的name属性名称必须等于pojo的属性名称
public ModelAndView updateitem(Items items){

itemsService.updateitems(items);

//不可以加斜杠 解析不了 itemList.action
return new ModelAndView(new RedirectView("itemList.action"));
}
7.重定向
?
1
2
3
4
5
6
7
8
@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传递数据。
以上这篇SpringMvc接收参数方法总结(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值