1.项目结构
2.视图页面
①userInfor_add1.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>Insert title here</title> </head> <body> <h1>添加用户信息3!!</h1> <form action="userInfor_add1.do" method="post"> 编号:<input type="text" name="userId"/><br> 姓名:<input type="text" name="userName"/><br> 性别:<input type="text" name="userSex"/><br> 兴趣爱好: <input type="checkbox" name="userXq" value="LOL"/>LOL <input type="checkbox" name="userXq" value="英雄联盟"/>英雄联盟 <input type="checkbox" name="userXq" value="DOTA"/>DOTA<br> <input type="submit" value="提交"> </form> </body> </html>
②userInfor_add2.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>Insert title here</title> </head> <body> <h1>添加用户信息3!!</h1> <form action="userInfor_add2.do" method="post"> 编号:<input type="text" name="userId"/><br> 姓名:<input type="text" name="userName"/><br> 性别:<input type="text" name="userSex"/><br> 兴趣爱好: <input type="checkbox" name="userXq" value="LOL"/>LOL <input type="checkbox" name="userXq" value="英雄联盟"/>英雄联盟 <input type="checkbox" name="userXq" value="DOTA"/>DOTA<br> 时间:<input type="text" name="userTime"/><br> <input type="submit" value="提交"> </form> </body> </html>
3.controller层代码
package com.spring; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.spring.po.UserInfor; @Controller /*@RequestMapping("/user")*/ public class UserInforController { @RequestMapping(value="/userInfor_add1.do",method=RequestMethod.POST) public String add(UserInfor user){ System.out.println(user); return "user/success"; } @RequestMapping(value="/userInfor_add2.do",method=RequestMethod.POST) public String add1(UserInfor user){ System.out.println(user); return "user/success"; } /** * 时间属性编辑器 * @param bin */ @InitBinder public void initBinder(ServletRequestDataBinder bin){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); CustomDateEditor cust=new CustomDateEditor(sdf, true); bin.registerCustomEditor(Date.class, cust); } }
4.po层代码
package com.spring.po; import java.util.Arrays; import java.util.Date; public class UserInfor { private Integer userId; private String userName; private String userSex; private String[] userXq; private Date userTime; public Date getUserTime() { return userTime; } public void setUserTime(Date userTime) { this.userTime = userTime; } public String[] getUserXq() { return userXq; } public void setUserXq(String[] userXq) { this.userXq = userXq; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserSex() { return userSex; } public void setUserSex(String userSex) { this.userSex = userSex; } @Override public String toString() { return "UserInfor [userId=" + userId + ", userName=" + userName + ", userSex=" + userSex + ", userXq=" + Arrays.toString(userXq) + ", userTime=" + userTime + "]"; } }
5.控制台打印
注:重点是对象的传入,时间的传入, 时间属性编辑器的配置,视图层与po层映射关系(复选框对应数组)