@RequestMapping("/test1.do")
public ModelAndView test1(HttpServletRequest request){
String userName =request.getParame("userName");//需要自己处理数据类型转换
String password = request.getParame("password");
return new ModelAndView("jsp/hello");
}
2.使用方法参数接收参数
@RequestMapping("/test2.do")
public ModelAndView test2(String userName,@RequestParam("password") String pwd){
//参数类型自动转换,有可能出现类型转换异常
return new ModelAndView("jsp/hello");
}
3.使用对象接收参数(使用自动机制封装成Bean对象)
/**需要定义User实体,属性名与<form>表单组件的name相同
页面:name,pwd
实体类:name,pwd
一一对应
*/
@RequestMapping("/test3.do")
public ModelAndView test3(User user){
return new ModelAndView("jsp/hello");
}
像页面发送数据的方法:
1.使用ModeAndView对象传送数据
@RequestMapping("/test4.do")
public ModelAndView test4(){
map<String,Object> data =new HashMap<String,Object>();
data.put("success",true);
data.put("message","操作成功");
return new ModelAndView("jsp/hello",data);
}
2.使用ModeMap传出数据
@RequestMapping("/test5.do")
public ModelAndView test5(ModelMap model){
model.addAttibute("success",false);
model.addAttibute("message","操作失败");
return new ModelAndView("jsp/hello");
}
3.使用@ModelAtrribute传出bean属性
@ModelAtrribute("age")//在Bean属性方法上使用
public int getAge(){
return age;
}
4.使用@ModelAtrribute传出参数值
@RequestMapping("/test6.do")//在Controller方法的参数部分
public ModelAndView test6(@ModelAtrribute("userName") String userName,String password){
return new ModelAndView("jsp/hello");
}