值传递的方式
作者:互联网
客户端给服务器端传递值,服务器端如何获取?
1)request
http://localhost:8080/SpringMVC/test02?name=tony&age=8
如果传递多个参数,直接定义为数组即可,参数名和方法形参保持一致
如果页面传递的参数名和值与形参不一致怎么办
@RequestMapping("/test04")
public String test04(@RequestParam("username") String name, @RequestParam("password") String pwd){
System.out.println("name:"+name+",password:"+pwd);
return "hello";
}
}
2)传递值的参数名和方法的形参名字保持一致
@RequestMapping("/test02")
public String test02(String name,Integer age){
System.out.println("name:"+name+",age:"+age);
return "hello";
}
数据共享:
服务器给客户端传递值
通过 HttpServletRequest 接收请求参数适用于 get 和 post 提交请求方式,示例代码如下:
@RequestMapping("/getval01")
public String getVal01(HttpServletRequest request){
request.setAttribute("name","tony");
request.setAttribute("dept",new Dept("测试部",100,"测试相关工资"));
return "getValue";
}
通过 ModelAndView
@RequestMapping("/getval02")
public ModelAndView getVal02(HttpSession session){
ModelAndView mv=new ModelAndView();
mv.addObject("name","jerry");
mv.addObject("dept",new Dept("人事部",10,"招聘"));
mv.setViewName("getValue");
session.setAttribute("name","session2");
return mv;
}
其他方式:
@RequestMapping("/getval03")
public String getval03(Model model){
model.addAttribute("name","model");
return "getValue";
}
@RequestMapping("/getval04")
public String getval04(Map<String,Object>map){
map.put("name","map");
return "getValue";
}
//全部赋值在request作用域
@RequestMapping("/getval05")
public String getval04(ModelMap modelMap){
modelMap.addAttribute("name","modelMap");
return "getValue";
}
标签:RequestMapping,return,name,方式,getValue,传递,public,String 来源: https://blog.csdn.net/weixin_47586838/article/details/120423838