介绍
Spring MVC的数据绑定功能做的很是强大,提供开发者以多种形式去获取页面传递过来的参数,今天我们一起看下spring支持以哪些方式获取值
①当我们业务场景是根据user的id去删除user的时候,url可能是如下几种
localhost:8080/spring-mvc/user/delete/1
localhost:8080/spring-mvc/user/delete/2
localhost:8080/spring-mvc/user/delete/3
根据我们上一节讲解的URL映射,我们可以写一个rest风格的映射,利用注解@RequestMapping,代码如下
package org.study.lyncc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value="/user") public class UserController { @RequestMapping(value="/delete/{id}") public ModelAndView delete(@PathVariable("id") Integer params){ System.out.println(params); ModelAndView mav = new ModelAndView("test"); mav.addObject("deleteId", params); return mav; } }跳转的test.jsp页面我们也修改一下
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>hello</title> </head> <body> ${deleteId} </body> </html>
这样我们就可以获取到了映射到了,但是我们想获取URL中的用户id例如1,2,3这样的值(就是/delete/{id}中的id值),我们可以利用Spring提供的@PathVariable注解,需要注意的是{id}中大括号中的id必须和@RequestMapping中的名称保持一致,如上代码显示,那么spring会自动把页面传递的值绑定到params这个变量上,当你的url是http://localhost:8080/spring-mvc/user/delete/123123的时候,运行结果:
我们也可以简便一下写法,我们平时开发过程中通常是这样的
我们一般不指定@PathVariable的value,只需要把变量名与path中的名一样一样可以映射到
②我们以前开发的时候删除一个用户的URL通常是这样的
localhost:8080/spring-mvc/delete/user?id=1
localhost:8080/spring-mvc/delete/user?id=2
localhost:8080/spring-mvc/delete/user?id=345
我们想得到问号后面的值,我们可以使用@RequestParam这样的属性
package org.study.lyncc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; @Controller public class UserController { @RequestMapping(value="/delete/user") public ModelAndView delete(@RequestParam(value="id") Integer params){ System.out.println(params); ModelAndView mav = new ModelAndView("test"); mav.addObject("deleteId", params); return mav; } }
我们再举一个更加复杂的例子,当我们的url是http://localhost:8080/spring-mvc/delete/user?id=1&username=lyncc&age=26的时候
package org.study.lyncc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; @Controller public class UserController { @RequestMapping(value="/delete/user") public ModelAndView delete(@RequestParam Integer id,@RequestParam String username,@RequestParam Integer age){ ModelAndView mav = new ModelAndView("test"); mav.addObject("deleteId", "id:"+id+"==username:"+username+"===age:"+age); return mav; } }注意我们也去掉了@RequestParam的value值,我们只需要一一对应值就可以了
③我们平时开发的时候,经常Ajax请求,例如我们写个ajax请求
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>hello</title> <script type="text/javascript" src="js/jquery-1.7.2.js"></script> </head> <body> </body> <script type="text/javascript"> $(function(){ var param = { "id":1, "username":"fly100%", "age":25 }; $.post("${pageContext.request.contextPath}/delete/user",param,function(data){ console.info(data); }); }); </script> </html>后台代码
package org.study.lyncc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class UserController { @RequestMapping(value="/test") public ModelAndView index(){ ModelAndView mav = new ModelAndView("test"); return mav; } @RequestMapping(value="/delete/user") public ModelAndView delete(Integer id,String username,Integer age){ ModelAndView mav = new ModelAndView("test"); System.out.println("id:"+id+"==username:"+username+"===age:"+age); mav.addObject("deleteId", "id:"+id+"==username:"+username+"===age:"+age); return mav; } }我们没有使用任何注解,运行结果:
发现依旧可以获取值
我们也可以这样,新建一个User对象
package org.study.lyncc.web.entity; public class User { private Integer id; private String username; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }修改一下Controller,把参数就替换成User对象
package org.study.lyncc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import org.study.lyncc.web.entity.User; @Controller public class UserController { @RequestMapping(value="/test") public ModelAndView index(){ ModelAndView mav = new ModelAndView("test"); return mav; } @RequestMapping(value="/delete/user") public ModelAndView delete(User u){ ModelAndView mav = new ModelAndView("test"); System.out.println("id:"+u.getId()+"==username:"+u.getUsername()+"===age:"+u.getAge()); mav.addObject("deleteId", "id:"+u.getId()+"==username:"+u.getUsername()+"===age:"+u.getAge()); return mav; } }
Spring MVC的数据绑定功能是非常强大的,一般常用的就是这些了~