#1.传入数据单个值(方式1)
没有使用视图解析器
controller代码以下html
@RequestMapping(value="/hello",method= RequestMethod.GET) public String hello(String name) throws IOException, ServletException{ System.out.println(name); return "index.jsp"; }
请求代码app
http://localhost:8080/ssm/hello?name=zhangsan
注意:请求的参数名(name)必须和接收参数(String name)对齐,不然接收不到。
#2.传入数据单个值(方式2)
使用@RequestParam接收参数。
请求代码同上
接收代码改成以下jsp
@RequestMapping(value="/hello",method= RequestMethod.GET) public String hello(@RequestParam("name")String userName) throws IOException, ServletException{ System.out.println(userName); return "index.jsp"; }
#3.传对象code
@RequestMapping(value="/hello",method= RequestMethod.GET) public String hello(User user) throws IOException, ServletException{ System.out.println(user); return "index.jsp"; }
User类以下htm
public class User implements Serializable{ private static final long serialVersionUID = 1L; private Integer id; private String name; }
传参以下对象
http://localhost:8080/ssm/hello?name=zhangsan&id=1001
#4.数据展现到前台(方式1:ModAndView)
使用视图解析器io
@RequestMapping(value="/hello",method= RequestMethod.GET) public ModelAndView hello(User user) throws IOException, ServletException{ ModelAndView mav = new ModelAndView(); mav.addObject("user", user); mav.setViewName("hello"); return mav; }
请求数据class
http://localhost:8080/ssm/hello?name=zhangsan&id=1001
hello.jsp请求
<html> <body> <h2>Hello World!</h2> userId:${user.id}<br> userName:${user.name} </body> </html>
结果im
Hello World! userId:1001 userName:zhangsan
#5.数据展现到前台(方式2:ModleMap)
不须要使用视图解析器
@RequestMapping(value="/hello",method= RequestMethod.GET) public String hello(User user,ModelMap modlMap) throws IOException, ServletException{ modlMap.addAttribute("user", user); return "index.jsp"; }
请求数据
http://localhost:8080/ssm/hello?name=zhangsan&id=1001
index.jsp
<html> <body> <h2>Hello World!</h2> userId:--- ${user.id}<br> userName:--- ${user.name} </body> </html>
结果
Hello World! userId:--- 1001 userName:--- zhangsan
ModelAndView与ModelMap的区别 1.前者须要视图解析器,然后者不须要 。 2.前者能够设值且指定跳转的页面,然后者只能设值。