接收值--四种方法:
第一种:参数直接写在controller参数列表中
@RequestMapping("/test1.action")
public ModelAndView test1(String name){
System.out.println(name);
return null;
}
第二种:request
@RequestMapping("/test2.action")
public ModelAndView test2(HttpServletRequest request){
System.out.println(request.getParameter("name"));
return null;
}
第三种:指定传参
@RequestMapping("/test3.action")
public ModelAndView test3(@RequestParam("name3") String name){
System.out.println(name);
return null;
}
第四种:通过对象
@RequestMapping("/test4.action")
public ModelAndView test4(User user){
System.out.println(user);
return null;
}
传值--三种方法:
第一种:request
@RequestMapping("/test5.action")
public String test5(HttpServletRequest request,User user){
request.setAttribute("message", user.getName());
return null;
}
第二种:ModelAndView
@RequestMapping("/test6.action")
public ModelAndView test6(User user){
System.out.println(user);
ModelAndView mv = new ModelAndView();
mv.addObject("message", user.getName() );
mv.setViewName("/index1.jsp");
return mv;
}
第三种:Model
@RequestMapping("/test7.action")
public String test7(User user,Model model){
System.out.println(user);
model.addAttribute("message", user.getName() + " hello");
return "/index1.jsp";
}
注:springmvc传值不止这三种方法
本文介绍了SpringMVC框架中四种接收数据的方法和三种传递数据的方法,包括使用控制器参数、请求对象、指定参数名及对象传参等方式,并详细解释了每种方式的具体实现。
3264

被折叠的 条评论
为什么被折叠?



