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& ...
随机推荐
- poj 1039
#include <iostream> #include <algorithm> #include <cstring> #include <cstdlib&g ...
- [Python学习笔记] 字符串类型及操作
字符串处理 索引:返回字符串中单个字符 <字符串>[M] 切片:返回字符串中一段字符子串 <字符串>[M:N:K] 字符串格式化使用.format()方法
- C++插入排序
直接插入排序是一种简单的插入排序法,适用于少量数据的排序,是一种较为稳定的排序算法,本文通过插入排序的方法实现对一个数组进行从大到小和从小到大的排序. 1. 从小到大的插入排序: 例如:给定整型数组a ...
- 恢复win7快捷方式小箭头
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Icons&q ...
- JavaScript判断该对象是否为数组
typeof 用来检测数据类型,Function, String, Number, Undefined都可以使用typeof来判断. function test(){} console.log(typ ...
- edgedb 内部pg 数据存储的探索 (三) 源码包setup.py 文件
edgedb 是基于python开发的,同时集成了cython 以下为包的setup.py 配置,从里面我们可以看到关于edgedb 的一些依赖 以及构建过程 setup.py 源码 整体配置不算很多 ...
- *&p理解
要明白这个需明白两个基础: 运算符*优先级高于&, 两个运算符都是从右向左结合运算 所以,*&a 的意思就是先运算 *,得到 指针,再通过 &,获取指针的引用 如果不理解,继续 ...
- Go外包 Go语言外包 Golang外包商 浅谈Go的全局变量和生命周期
最近做Go语言开发,有些心得分享下: Go语言全局var不同于PHP里的全局var,最大区别session 还是app,app 所有人都可以改.session自己改.不同过程之间通用 比如我php ...
- 配置Tomcat使用https协议(配置SSL协议)
配置Tomcat使用https协议(配置SSL协议) 2014-01-20 16:38 58915人阅读 评论(3) 收藏 举报 转载地址:http://ln-ydc.iteye.com/blog/1 ...
- MicrosoftRootCertificateAuthority2011.cer 下载
下载地址:https://files.cnblogs.com/files/hyh123/microsoft-root-certificate-authority.rar 在安装Microsoft .N ...