在Spring MVC中,前端JSP页面可以传递 基本类型(int,String)、实体类型、包装类型、数组类型、集合类型(List、map )等。
假如在传递的类型中有 Date类型的字段,需要在 Controller 通过 initBinder() 进行处理,代码如下:
@Controller public class userController { /* * 添加用户 * 通过基本参数封装获取参数 */ @RequestMapping(value = "/user/addUser2", method = RequestMethod.POST) public ModelAndView addUser2(String username,String usercode,Date birthday,String address) { ModelAndView modelAndView = new ModelAndView(); userModel model = new userModel(); model.setUserName(username); model.setUserCode(usercode); model.setBirthday(birthday); model.setAddress(address); modelAndView.setViewName("/user/list"); modelAndView.addObject("user", model); return modelAndView; } //处理日期类型参数 @InitBinder protected void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); } }
1、HttpServletRequest 获取参数
JSP页面:
<div style=" 28%; float: left;"> <fieldset> <legend>普通提交-request.getParameter(args) 获取参数</legend> <form action="${pageContext.request.contextPath }/user/addUser" method="post"> <div style=" 200px"> <label>名字:</label> <input name="username" id="username" placeholder="请输入名字" /> </div> <div style=" 200px"> <label>编号:</label> <input name="usercode" id="usercode" placeholder="请输入编号" /> </div> <div style=" 200px"> <label>生日:</label> <input name="birthday" id="birthday" placeholder="请输入生日" /> </div> <div style=" 200px"> <label>地址:</label> <input name="address" id="address" placeholder="请输入地址" /> </div> <div style=" 200px"> <button type="reset">重置</button> <button type="submit">提交</button> </div> </form> </fieldset> </div>
Controller:
/* * 添加用户 * 通过 request.getParameter(args) 获取参数 */ @RequestMapping(value = "/user/addUser", method = RequestMethod.POST) public ModelAndView addUser(HttpServletRequest request, HttpServletResponse response) { ModelAndView modelAndView = new ModelAndView(); userModel model = new userModel(); model.setUserName(request.getParameter("username").toString()); model.setUserCode(request.getParameter("usercode").toString()); model.setAddress(request.getParameter("address").toString()); modelAndView.setViewName("/user/list"); modelAndView.addObject("user", model); return modelAndView; }
2、基本类型获取参数
JSP页面:
<div style=" 28%; float: left;"> <fieldset> <legend>普通提交-基本参数取值</legend> <form action="${pageContext.request.contextPath }/user/addUser2" method="post"> <div style=" 200px"> <label>名字:</label> <input name="username" id="username" placeholder="请输入名字" /> </div> <div style=" 200px"> <label>编号:</label> <input name="usercode" id="usercode" placeholder="请输入编号" /> </div> <div style=" 200px"> <label>生日:</label> <input name="birthday" id="birthday" placeholder="请输入生日" /> </div> <div style=" 200px"> <label>地址:</label> <input name="address" id="address" placeholder="请输入地址" /> </div> <div style=" 200px"> <button type="reset">重置</button> <button type="submit">提交</button> </div> </form> </fieldset> </div>
Controller:
/* * 添加用户 * 通过基本参数封装获取参数 */ @RequestMapping(value = "/user/addUser2", method = RequestMethod.POST) public ModelAndView addUser2(String username,String usercode,Date birthday,String address) { ModelAndView modelAndView = new ModelAndView(); userModel model = new userModel(); model.setUserName(username); model.setUserCode(usercode); model.setBirthday(birthday); model.setAddress(address); modelAndView.setViewName("/user/list"); modelAndView.addObject("user", model); return modelAndView; }
4、实体(javaBean)参数获取参数
实体:
1 public class userModel { 2 3 @Override 4 public String toString() { 5 return "userModel [userName=" + userName + ", userCode=" + userCode + ", account=" + account + ", pwd=" + pwd 6 + ", address=" + address + ", birthday=" + birthday + ", Memo=" + Memo + "]"; 7 } 8 9 private String userName; 10 private String userCode; 11 private String account; 12 private String pwd; 13 private String address; 14 private Date birthday; 15 private String Memo; 16 17 public String getAccount() { 18 return account; 19 } 20 21 public void setAccount(String account) { 22 this.account = account; 23 } 24 25 public String getPwd() { 26 return pwd; 27 } 28 29 public void setPwd(String pwd) { 30 this.pwd = pwd; 31 } 32 33 public String getMemo() { 34 return Memo; 35 } 36 37 public void setMemo(String memo) { 38 Memo = memo; 39 } 40 41 public Date getBirthday() { 42 return birthday; 43 } 44 45 public void setBirthday(Date birthday) { 46 this.birthday = birthday; 47 } 48 49 public String getUserName() { 50 return userName; 51 } 52 53 public void setUserName(String userName) { 54 this.userName = userName; 55 } 56 57 public String getUserCode() { 58 return userCode; 59 } 60 61 public void setUserCode(String userCode) { 62 this.userCode = userCode; 63 } 64 65 public String getAddress() { 66 return address; 67 } 68 69 public void setAddress(String address) { 70 this.address = address; 71 } 72 73 }
JSP页面:
<div style=" 28%; float: left;"> <fieldset> <legend>通过javaBean获取参数</legend> <form action="${pageContext.request.contextPath }/user/addUser3" method="post"> <div style=" 200px"> <label>名字:</label> <input name="userName" id="userName" placeholder="请输入名字" /> </div> <div style=" 200px"> <label>编号:</label> <input name="userCode" id="userCode" placeholder="请输入编号" /> </div> <div style=" 200px"> <label>生日:</label> <input name="birthday" id="birthday" placeholder="请输入生日" /> </div> <div style=" 200px"> <label>地址:</label> <input name="address" id="address" placeholder="请输入地址" /> </div> <div style=" 200px"> <button type="reset">重置</button> <button type="submit">提交</button> </div> </form> </fieldset> </div>
Controller:
/* * 添加用户 * 通过javaBean获取参数 */ @RequestMapping("/user/addUser3") public ModelAndView addUser3(userModel model) { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/user/list"); modelAndView.addObject("user", model); return modelAndView; }
5、包装类获取参数
实体类:
1 public class userModel { 2 3 @Override 4 public String toString() { 5 return "userModel [userName=" + userName + ", userCode=" + userCode + ", account=" + account + ", pwd=" + pwd 6 + ", address=" + address + ", birthday=" + birthday + ", Memo=" + Memo + "]"; 7 } 8 9 private String userName; 10 private String userCode; 11 private String account; 12 private String pwd; 13 private String address; 14 private Date birthday; 15 private String Memo; 16 17 public String getAccount() { 18 return account; 19 } 20 21 public void setAccount(String account) { 22 this.account = account; 23 } 24 25 public String getPwd() { 26 return pwd; 27 } 28 29 public void setPwd(String pwd) { 30 this.pwd = pwd; 31 } 32 33 public String getMemo() { 34 return Memo; 35 } 36 37 public void setMemo(String memo) { 38 Memo = memo; 39 } 40 41 public Date getBirthday() { 42 return birthday; 43 } 44 45 public void setBirthday(Date birthday) { 46 this.birthday = birthday; 47 } 48 49 public String getUserName() { 50 return userName; 51 } 52 53 public void setUserName(String userName) { 54 this.userName = userName; 55 } 56 57 public String getUserCode() { 58 return userCode; 59 } 60 61 public void setUserCode(String userCode) { 62 this.userCode = userCode; 63 } 64 65 public String getAddress() { 66 return address; 67 } 68 69 public void setAddress(String address) { 70 this.address = address; 71 } 72 73 }
1 public class userModelArray { 2 private userModel model; 3 4 private String code; 5 6 public userModel getModel() { 7 return model; 8 } 9 10 public void setModel(userModel model) { 11 this.model = model; 12 } 13 14 public String getCode() { 15 return code; 16 } 17 18 public void setCode(String code) { 19 this.code = code; 20 } 21 22 @Override 23 public String toString() { 24 return "userModelArray [model=" + model + ", code=" + code + "]"; 25 } 26 }
JSP页面:
<div style=" 28%; float: left;"> <fieldset> <legend>通过包装类获取参数</legend> <form action="${pageContext.request.contextPath }/user/addUser4" method="post"> <div style=" 200px"> <label>名字:</label> <input name="model.userName" id="userName" placeholder="请输入名字" /> </div> <div style=" 200px"> <label>编号:</label> <input name="code" id="code" placeholder="请输入编号" /> </div> <div style=" 200px"> <label>生日:</label> <input name="model.birthday" id="birthday" placeholder="请输入生日" /> </div> <div style=" 200px"> <label>地址:</label> <input name="model.address" id="address" placeholder="请输入地址" /> </div> <div style=" 200px"> <button type="reset">重置</button> <button type="submit">提交</button> </div> </form> </fieldset> </div>
Controller:
/* * 添加用户 * 通过包装类获取参数 */ @RequestMapping("/user/addUser4") public ModelAndView addUser4(userModelArray model) { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/user/list"); modelAndView.addObject("user", model); return modelAndView; }
6、数组类型获取参数
JSP页面:
<div style=" 28%; float: left;"> <fieldset> <legend>数组类型获取参数</legend> <form action="${pageContext.request.contextPath }/user/addUser5" method="post"> <div style=" 200px"> <label>选择:</label> 张三<input name="id" id="id" type="checkbox" value="1" /> 李四<input name="id" id="id" type="checkbox" value="2" /> 王五<input name="id" id="id" type="checkbox" value="3" /> 赵六<input name="id" id="id" type="checkbox" value="4" /> </div> <div style=" 200px"> <button type="reset">重置</button> <button type="submit">提交</button> </div> </form> </fieldset> </div>
Controller:
/* * 添加用户 * 数组类型获取参数 */ @RequestMapping("/user/addUser5") public ModelAndView addUser5(Integer[] id) { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/user/list"); modelAndView.addObject("user", new userModel()); return modelAndView; }
7、包装类-List集合 获取参数传递
JSP页面:
<div style=" 28%; float: left;"> <fieldset> <legend>封装类集合获取参数</legend> <form action="${pageContext.request.contextPath }/user/addUser6" method="post"> <div style=" 200px"> <label>名字:</label> <input name="userList[0].userName" id="userList[0].userName" placeholder="请输入名字" /> </div> <div style=" 200px"> <label>编号:</label> <input name="userList[0].userCode" id="userList[0].userCode" placeholder="请输入编号" /> </div> <div style=" 200px"> <label>生日:</label> <input name="userList[0].birthday" id="birthday" placeholder="请输入生日" /> </div> <div style=" 200px"> <label>地址:</label> <input name="userList[0].address" id="address" placeholder="请输入地址" /> </div> <div style=" 200px"> <label>名字:</label> <input name="userList[1].userName" id="userList[0].userName" placeholder="请输入名字" /> </div> <div style=" 200px"> <label>编号:</label> <input name="userList[1].userCode" id="userList[0].userCode" placeholder="请输入编号" /> </div> <div style=" 200px"> <label>生日:</label> <input name="userList[1].birthday" id="birthday" placeholder="请输入生日" /> </div> <div style=" 200px"> <label>地址:</label> <input name="userList[1].address" id="address" placeholder="请输入地址" /> </div> <div style=" 200px"> <button type="reset">重置</button> <button type="submit">提交</button> </div> </form> </fieldset> </div>
模型类:
1 /* 2 * 包装类 - 包装 List<userModel> 3 */ 4 public class UserModelCustom { 5 // 集合 6 private List<userModel> userList; 7 8 public List<userModel> getUserList() { 9 return userList; 10 } 11 public void setUserList(List<userModel> userList) { 12 this.userList = userList; 13 } 14 } 15 /* 16 * userModel 类 17 */ 18 public class userModel { 19 private String userName; 20 private String userCode; 21 private String account; 22 private String pwd; 23 private String address; 24 private Date birthday; 25 private String Memo; 26 27 public String getAccount() { 28 return account; 29 } 30 31 public void setAccount(String account) { 32 this.account = account; 33 } 34 35 public String getPwd() { 36 return pwd; 37 } 38 39 public void setPwd(String pwd) { 40 this.pwd = pwd; 41 } 42 43 public String getMemo() { 44 return Memo; 45 } 46 47 public void setMemo(String memo) { 48 Memo = memo; 49 } 50 51 public Date getBirthday() { 52 return birthday; 53 } 54 55 public void setBirthday(Date birthday) { 56 this.birthday = birthday; 57 } 58 59 public String getUserName() { 60 return userName; 61 } 62 63 public void setUserName(String userName) { 64 this.userName = userName; 65 } 66 67 public String getUserCode() { 68 return userCode; 69 } 70 71 public void setUserCode(String userCode) { 72 this.userCode = userCode; 73 } 74 75 public String getAddress() { 76 return address; 77 } 78 79 public void setAddress(String address) { 80 this.address = address; 81 } 82 }
Controller:
/* * 添加用户 * 封装类集合获取参数 */ @RequestMapping("/user/addUser6") public String addUser6(UserModelCustom customModel) { return "/user/list"; }
8、包装类-Map集合 获取参数传递
JSP页面:
<div style=" 28%; float: left;"> <fieldset> <legend>封装类Map获取参数</legend> <form action="${pageContext.request.contextPath }/user/addUser7" method="post"> <div style=" 200px"> <label>名字:</label> <input name="maps['userName']" id="map['userName']" placeholder="请输入名字" /> </div> <div style=" 200px"> <label>编号:</label> <input name="maps['userCode']" id="map['userCode']" placeholder="请输入编号" /> </div> <div style=" 200px"> <label>生日:</label> <input name="maps['birthday']" id="map['birthday']" placeholder="请输入生日" /> </div> <div style=" 200px"> <label>地址:</label> <input name="maps['address']" id="map['address']" placeholder="请输入地址" /> </div> <div style=" 200px"> <button type="reset">重置</button> <button type="submit">提交</button> </div> </form> </fieldset> </div>
模型类:
1 /* 2 * 包装类 - 包装 List<userModel> 3 */ 4 public class UserModelCustom { 5 // map 6 private Map<String, Object> maps; 7 8 public Map<String, Object> getMaps() { 9 return maps; 10 } 11 public void setMaps(Map<String, Object> maps) { 12 this.maps = maps; 13 } 14 } 15 /* 16 * userModel 类 17 */ 18 public class userModel { 19 private String userName; 20 private String userCode; 21 private String account; 22 private String pwd; 23 private String address; 24 private Date birthday; 25 private String Memo; 26 27 public String getAccount() { 28 return account; 29 } 30 31 public void setAccount(String account) { 32 this.account = account; 33 } 34 35 public String getPwd() { 36 return pwd; 37 } 38 39 public void setPwd(String pwd) { 40 this.pwd = pwd; 41 } 42 43 public String getMemo() { 44 return Memo; 45 } 46 47 public void setMemo(String memo) { 48 Memo = memo; 49 } 50 51 public Date getBirthday() { 52 return birthday; 53 } 54 55 public void setBirthday(Date birthday) { 56 this.birthday = birthday; 57 } 58 59 public String getUserName() { 60 return userName; 61 } 62 }
Controller:
/* * 添加用户 * 封装类Map获取参数 */ @RequestMapping("/user/addUser7") public String addUser7(UserModelCustom customModel) { return "/user/list"; }