1.提交数据的处理
提交的域名称和处理方法的参数一致即可。
http://localhost:8088/SpringMvcData/hello.do?<span style="color:#993300;">name=zhangsan</span>处理方法:
@RequestMapping("hello.do")
public String hello(String name){
System.out.println(name);
return "index.jsp";
}当域的名称和方法的名称不同时代码:
/**
* @RequestParam("uname") 中的uname是提交域的名称
* @param name
* @return
*/
@RequestMapping("hello.do")
public String hello(@RequestParam("uname")String name){
System.out.println(name);
return "index.jsp";
}提交一个对象
要求提交的表单域名和对象中的属性名一致,参数使用对象即可
http://localhost:8088/SpringMvcData/hello.do?<span style="color:#ff0000;">name=zhangsan&id=123</span>处理方法:
@RequestMapping("hello.do")
public String hello(User user){
System.out.println(user.getName()+"---"+user.getId());
return "index.jsp";
}实体类:
public class User {
private int id;
private String name;
private String age;
//省略get、set方法
}
2.把数据显示到UI层:
第一种通过ModelAndView----需要视图解析器
ModelAndView mav = new ModelAndView("index");
mav.addObject("hello", username);
return mav;第二种通过ModelMap来实现----不需要视图解析器:
@RequestMapping("hello.do")
public String hello(String name, ModelMap md){
md.addAttribute("name", name);
return "index.jsp";
}
这里要注意ModelMap只能在参数中 不能再方法中new 在方法中new是不好使的!!!
本文详细介绍了Spring MVC框架中提交数据的处理方法,包括处理相同和不同域名的数据,以及如何将数据显示到UI层,包括使用ModelAndView和ModelMap的方式。同时,文章还涉及了如何将对象作为参数进行传递。
4366

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



