Spring MVC参数封装传递
在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="width: 28%; float: left;">
<fieldset>
<legend>普通提交-request.getParameter(args) 获取参数</legend>
<form action="${pageContext.request.contextPath }/user/addUser"
method="post">
<div style="width: 200px">
<label>名字:</label> <input name="username" id="username"
placeholder="请输入名字" />
</div>
<div style="width: 200px">
<label>编号:</label> <input name="usercode" id="usercode"
placeholder="请输入编号" />
</div>
<div style="width: 200px">
<label>生日:</label> <input name="birthday" id="birthday"
placeholder="请输入生日" />
</div>
<div style="width: 200px">
<label>地址:</label> <input name="address" id="address"
placeholder="请输入地址" />
</div>
<div style="width: 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="width: 28%; float: left;">
<fieldset>
<legend>普通提交-基本参数取值</legend>
<form action="${pageContext.request.contextPath }/user/addUser2"
method="post">
<div style="width: 200px">
<label>名字:</label> <input name="username" id="username"
placeholder="请输入名字" />
</div>
<div style="width: 200px">
<label>编号:</label> <input name="usercode" id="usercode"
placeholder="请输入编号" />
</div>
<div style="width: 200px">
<label>生日:</label> <input name="birthday" id="birthday"
placeholder="请输入生日" />
</div>
<div style="width: 200px">
<label>地址:</label> <input name="address" id="address"
placeholder="请输入地址" />
</div>
<div style="width: 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)参数获取参数
实体:
public class userModel {
@Override
public String toString() {
return "userModel [userName=" + userName + ", userCode=" + userCode + ", account=" + account + ", pwd=" + pwd
+ ", address=" + address + ", birthday=" + birthday + ", Memo=" + Memo + "]";
}
private String userName;
private String userCode;
private String account;
private String pwd;
private String address;
private Date birthday;
private String Memo;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getMemo() {
return Memo;
}
public void setMemo(String memo) {
Memo = memo;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
JSP页面:
<div style="width: 28%; float: left;">
<fieldset>
<legend>通过javaBean获取参数</legend>
<form action="${pageContext.request.contextPath }/user/addUser3"
method="post">
<div style="width: 200px">
<label>名字:</label> <input name="userName" id="userName"
placeholder="请输入名字" />
</div>
<div style="width: 200px">
<label>编号:</label> <input name="userCode" id="userCode"
placeholder="请输入编号" />
</div>
<div style="width: 200px">
<label>生日:</label> <input name="birthday" id="birthday"
placeholder="请输入生日" />
</div>
<div style="width: 200px">
<label>地址:</label> <input name="address" id="address"
placeholder="请输入地址" />
</div>
<div style="width: 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、包装类获取参数
实体类:
public class userModel {
@Override
public String toString() {
return "userModel [userName=" + userName + ", userCode=" + userCode + ", account=" + account + ", pwd=" + pwd
+ ", address=" + address + ", birthday=" + birthday + ", Memo=" + Memo + "]";
}
private String userName;
private String userCode;
private String account;
private String pwd;
private String address;
private Date birthday;
private String Memo;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getMemo() {
return Memo;
}
public void setMemo(String memo) {
Memo = memo;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public class userModelArray {
private userModel model;
private String code;
public userModel getModel() {
return model;
}
public void setModel(userModel model) {
this.model = model;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "userModelArray [model=" + model + ", code=" + code + "]";
}
}
JSP页面:
<div style="width: 28%; float: left;">
<fieldset>
<legend>通过包装类获取参数</legend>
<form action="${pageContext.request.contextPath }/user/addUser4"
method="post">
<div style="width: 200px">
<label>名字:</label> <input name="model.userName" id="userName"
placeholder="请输入名字" />
</div>
<div style="width: 200px">
<label>编号:</label> <input name="code" id="code"
placeholder="请输入编号" />
</div>
<div style="width: 200px">
<label>生日:</label> <input name="model.birthday" id="birthday"
placeholder="请输入生日" />
</div>
<div style="width: 200px">
<label>地址:</label> <input name="model.address" id="address"
placeholder="请输入地址" />
</div>
<div style="width: 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="width: 28%; float: left;">
<fieldset>
<legend>数组类型获取参数</legend>
<form action="${pageContext.request.contextPath }/user/addUser5"
method="post">
<div style="width: 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="width: 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="width: 28%; float: left;">
<fieldset>
<legend>封装类集合获取参数</legend>
<form action="${pageContext.request.contextPath }/user/addUser6" method="post">
<div style="width: 200px">
<label>名字:</label> <input name="userList[0].userName" id="userList[0].userName" placeholder="请输入名字" />
</div>
<div style="width: 200px">
<label>编号:</label> <input name="userList[0].userCode" id="userList[0].userCode" placeholder="请输入编号" />
</div>
<div style="width: 200px">
<label>生日:</label> <input name="userList[0].birthday" id="birthday" placeholder="请输入生日" />
</div>
<div style="width: 200px">
<label>地址:</label> <input name="userList[0].address" id="address" placeholder="请输入地址" />
</div>
<div style="width: 200px">
<label>名字:</label> <input name="userList[1].userName" id="userList[0].userName" placeholder="请输入名字" />
</div>
<div style="width: 200px">
<label>编号:</label> <input name="userList[1].userCode" id="userList[0].userCode" placeholder="请输入编号" />
</div>
<div style="width: 200px">
<label>生日:</label> <input name="userList[1].birthday" id="birthday" placeholder="请输入生日" />
</div>
<div style="width: 200px">
<label>地址:</label> <input name="userList[1].address" id="address" placeholder="请输入地址" />
</div>
<div style="width: 200px">
<button type="reset">重置</button>
<button type="submit">提交</button>
</div>
</form>
</fieldset>
</div>
模型类:
/*
* 包装类 - 包装 List<userModel>
*/
public class UserModelCustom {
// 集合
private List<userModel> userList; public List<userModel> getUserList() {
return userList;
}
public void setUserList(List<userModel> userList) {
this.userList = userList;
}
}
/*
* userModel 类
*/
public class userModel {
private String userName;
private String userCode;
private String account;
private String pwd;
private String address;
private Date birthday;
private String Memo; public String getAccount() {
return account;
} public void setAccount(String account) {
this.account = account;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} public String getMemo() {
return Memo;
} public void setMemo(String memo) {
Memo = memo;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getUserCode() {
return userCode;
} public void setUserCode(String userCode) {
this.userCode = userCode;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
}
}
Controller:
/*
* 添加用户
* 封装类集合获取参数
*/
@RequestMapping("/user/addUser6")
public String addUser6(UserModelCustom customModel) { return "/user/list";
}
8、包装类-Map集合 获取参数传递
JSP页面:
<div style="width: 28%; float: left;">
<fieldset>
<legend>封装类Map获取参数</legend>
<form action="${pageContext.request.contextPath }/user/addUser7" method="post">
<div style="width: 200px">
<label>名字:</label> <input name="maps['userName']" id="map['userName']" placeholder="请输入名字" />
</div>
<div style="width: 200px">
<label>编号:</label> <input name="maps['userCode']" id="map['userCode']" placeholder="请输入编号" />
</div>
<div style="width: 200px">
<label>生日:</label> <input name="maps['birthday']" id="map['birthday']" placeholder="请输入生日" />
</div>
<div style="width: 200px">
<label>地址:</label> <input name="maps['address']" id="map['address']" placeholder="请输入地址" />
</div>
<div style="width: 200px">
<button type="reset">重置</button>
<button type="submit">提交</button>
</div>
</form>
</fieldset>
</div>
模型类:
/*
* 包装类 - 包装 List<userModel>
*/
public class UserModelCustom {
// map
private Map<String, Object> maps; public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
}
/*
* userModel 类
*/
public class userModel {
private String userName;
private String userCode;
private String account;
private String pwd;
private String address;
private Date birthday;
private String Memo; public String getAccount() {
return account;
} public void setAccount(String account) {
this.account = account;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} public String getMemo() {
return Memo;
} public void setMemo(String memo) {
Memo = memo;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getUserName() {
return userName;
}
}
Controller:
/*
* 添加用户
* 封装类Map获取参数
*/
@RequestMapping("/user/addUser7")
public String addUser7(UserModelCustom customModel) { return "/user/list";
}
Spring MVC参数封装传递的更多相关文章
- spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping
spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClas ...
- Spring MVC参数绑定(如何接收请求参数及返回参数)
在SpringMVC interceptor案例实践中遇到了获取jsp表单传递参数失败的问题,怎么的解决的呢?下面详细介绍. 先讲述下https://www.cnblogs.com/ilovebath ...
- spring mvc参数绑定
spring绑定参数的过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接 ...
- spring mvc 控制器方法传递一些经验对象的数组
由于该项目必须提交一个表单,其中多个对象,更好的方法是直接通过在控制器方法参数的数组. 因为Spring mvc框架在反射生成控制方法的參数对象的时候会调用这个类的getDeclaredConstru ...
- spring mvc 参数绑定
基础类型 原始类型:id必须要传,否则报错. @RequestMapping("/test") @ResponseBody public ResponseData test(int ...
- Spring MVC参数处理
使用Servlet API作为参数 HttpServletRequest HttpServletResponse HttpSession 使用流作为参数 总结 Spring MVC通过分析处理处理方法 ...
- spring mvc 参数
Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts: Struts是一个表示层框架,主要作用是界面展示,接收请求,分发请求. 在MVC框架中,Struts属于V ...
- Spring mvc参数类型转换
1,需求 有时候我们接收到的参数为String类型的,但是我们需要将它们转化为其他类型的如:date类型,枚举类型等等,spring mvc为我们提供了这样的功能. 2,配置文件 在springmvc ...
- Spring MVC 参数必填项导致客户端报 HTTP 400 并且无法进入断点的问题
1.问题 Spring MVC 在参数上设置了必填项,post 请求时报 HTTP 400 并且未进入断点,如将“年龄”设置为了必填项: @RequestParam( value="age& ...
随机推荐
- PIL库的总结及运用
PIL库的总结:(以代码形式) #date: 2018/11/15 from PIL import Image,ImageFilter,ImageDraw,ImageFont #####除了缩略图的方 ...
- Ex0203
游戏 – 这些软件的开发者是怎么说服你(陌生人)成为他们的用户的?他们的目标都是盈利么?他们的目标都是赚取用户的现金么?还是别的? 朋友们都在玩,我在试玩的时候也觉得很不错:游戏基本上的目标都 ...
- 基于Linux-3.9.4的mykernel实验环境的极简内核分析
382 + 原创作品转载请注明出处 + https://github.com/mengning/linuxkernel/ 一.实验环境 win10 -> VMware -> Ubuntu1 ...
- install rust
Step 1. Trial 1 Download rustup-init.exe exec rustup-init.exe SW hangs 2. Trial 2 install rust-1.33. ...
- FirewallD 快速使用文档
FirewallD简介 FirewallD是CentOS7系列上代替iptables管理netfilter的配置工具,提供图形化和命令行,使用python开发(新版中计划使用c++重写),提供图形化和 ...
- 图形化SVN管理搭建 subversion edge自行修改密码
参考文章: https://blog.csdn.net/buyaore_wo/article/details/84313467 安装版本: Subversion Edge 5.2.3 (Linux 6 ...
- EasyTouch和NGUI的使用心得
今天来写一写Unity3D中两个比较常用插件:EasyTouch和NGUI的学习心得.我用的版本分别是EasyTouch 3.1.1和NGUI 3.6.0,下面也是对这两个版本的学习心得. 1. Ea ...
- 闲话ACES(修订)
最近身边的人见面就聊ACES,ACES俨然已经是行业热点了. ACES的确更高效的解决了色彩一致性的问题,这是符合历史进程的(+1s),无疑值得肯定.但由于色彩管理意识不强,关于ACES的认识就存在着 ...
- html5课件外包-----swf/AS2/AS3/fla/ppt课件如何转换为html5交互课件/动画
随着Adobe公司公布2020年将不再更新和维护flash,flash逐渐被html5取代,很多教育机构都在面临着如何将自己的flash交互课件产品转换到html5版本的问题,最近遇到非常多的客户一上 ...
- 使用VISIO远程服务器上的ORACLE数据库,反向生成数据库实体关系图
反向即根据已有的数据库,生成ER图,很多工具都可以实现这一过程,如visio,powerdesigner等,下面文章记录一下我使用VISIO生成远程服务器上的一个数据库ER图过程,供以后自己参考. 1 ...