使用Spring MVC实现数据绑定
使用Spring MVC实现数据绑定
——Spring MVC支持将表单参数自动绑定在映射方法的参数。
①绑定标量数据
前端代码:
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。<form action="${pageContext.request.contextPath }/login.mvc" method="post"> 用户名:<input name="username" type="text"> 密码:<input name="password" type="text"> <input type="submit" value="登录"> </form>
后端代码:
1 @RequestMapping(value="login",method=RequestMethod.POST) 2 public String login(String password,String username){ 3 System.out.println("-登录-"+username); 4 return "/hello.jsp"; 5 }
②绑定Java Bean对象
前端代码:
<form action="${pageContext.request.contextPath }/arruser.mvc" method="post"> 用户名1:<input name="username" type="text"> 用户名2:<input name="username" type="text"> 用户名3:<input name="username" type="text"> <input type="submit" value="登录"> </form>
后端代码(JavaBean的类我就不写了)
1 @RequestMapping(value="arruser",method=RequestMethod.POST) 2 public String login(String[] username){ 3 System.out.println("-登录-"+ username[0]); 4 return "/hello.jsp"; 5 }
③绑定数组
前端代码<form action="${pageContext.request.contextPath }/arruser.mvc" method="post"> 用户名1:<input name="username" type="text"> 用户名2:<input name="username" type="text"> 用户名3:<input name="username" type="text"> <input type="submit" value="登录"> </form>
后端代码
1 @RequestMapping(value="arruser",method=RequestMethod.POST) 2 public String login(String[] username){ 3 System.out.println("-登录-"+ username[0]); 4 return "/hello.jsp"; 5 }
④绑定集合
前端代码
<form action="${pageContext.request.contextPath }/listuser.mvc" method="post"> 用户名1:<input name="username" type="text"> 密码1:<input name="password" type="text"> 用户名2:<input name="username" type="text"> 密码2:<input name="password" type="text"> <input type="submit" value="登录"> </form>
后端代码
1 public class UsersModel { 2 private List<User> users; 3 public List<User> getUsers() { 4 return users; 5 } 6 public void setUsers(List<User> users) { 7 this.users = users; 8 } 9 }
@RequestMapping(value="listuser",method=RequestMethod.POST) public String login(UsersModel userModel){ System.out.println("用户1:"+ userModel.getUsers()[0].getUsername()); return "/hello.jsp"; }
——注意事项
① Spring内置类型数据和标量类型绑定方法相同;②数组只支持Spring内置类型和标量类型的数据;③Spring MVC不支持将表单参数自动绑定在映射方法的集合参数,需要通过借助一个JavaBean的类型隐性实现
⑤数据的强制绑定
①@PathVariable注解:绑定路径参数; ②@CookieValue注解:绑定Cookie的值;
前端代码
<form action="${pageContext.request.contextPath}/zhangsan/login.action" method="post"> 密码:<input name="psw" type="text"> <input type="submit" value="登录"> </form>
后端代码
1 @RequestMapping(value="/{username}/login",method=RequestMethod.POST) 2 public String login(@PathVariable String username, @RequestParam("psw") String password, @CookieValue(value="JSESSIONID") String cookie){ 3 System.out.println("-登录-"+ cookie); 4 System.out.println("-用户-"+ username); 5 return "/hello.jsp"; 6 }
③@RequestParam注解:通过设置参数名绑定指定参数或绑定表单数据到Map容器。
前端代码
<form action="${pageContext.request.contextPath }/login.mvc" method="post"> 用户名:<input name="username" type="text"> 密码:<input name="password" type="text"> <input type="submit" value="登录"> </form>
后端代码
1 @RequestMapping(value="login",method=RequestMethod.POST) 2 public String login(@RequestParam Map<String,Object> user){ 3 System.out.println("-登录-"+ user.get("username")); 4 return "/hello.jsp"; 5 }

更多精彩