在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参数封装传递的更多相关文章

  1. spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping

    spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClas ...

  2. Spring MVC参数绑定(如何接收请求参数及返回参数)

    在SpringMVC interceptor案例实践中遇到了获取jsp表单传递参数失败的问题,怎么的解决的呢?下面详细介绍. 先讲述下https://www.cnblogs.com/ilovebath ...

  3. spring mvc参数绑定

    spring绑定参数的过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接 ...

  4. spring mvc 控制器方法传递一些经验对象的数组

    由于该项目必须提交一个表单,其中多个对象,更好的方法是直接通过在控制器方法参数的数组. 因为Spring mvc框架在反射生成控制方法的參数对象的时候会调用这个类的getDeclaredConstru ...

  5. spring mvc 参数绑定

    基础类型 原始类型:id必须要传,否则报错. @RequestMapping("/test") @ResponseBody public ResponseData test(int ...

  6. Spring MVC参数处理

    使用Servlet API作为参数 HttpServletRequest HttpServletResponse HttpSession 使用流作为参数 总结 Spring MVC通过分析处理处理方法 ...

  7. spring mvc 参数

    Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts: Struts是一个表示层框架,主要作用是界面展示,接收请求,分发请求. 在MVC框架中,Struts属于V ...

  8. Spring mvc参数类型转换

    1,需求 有时候我们接收到的参数为String类型的,但是我们需要将它们转化为其他类型的如:date类型,枚举类型等等,spring mvc为我们提供了这样的功能. 2,配置文件 在springmvc ...

  9. Spring MVC 参数必填项导致客户端报 HTTP 400 并且无法进入断点的问题

    1.问题 Spring MVC 在参数上设置了必填项,post 请求时报 HTTP 400 并且未进入断点,如将“年龄”设置为了必填项: @RequestParam( value="age& ...

随机推荐

  1. word2007无法打开.doc

    如果您的WORD2007无法打开.DOC文档,可以试试如下的方法 打开注册表编辑器(开始-运行-输入regedit VISTA中在开始菜单最下方的搜索栏内输入regedit) 展开HKEY_CLASS ...

  2. P2257 莫比乌斯+整除分块

    #include<bits/stdc++.h> #define ll long long using namespace std; ; int vis[maxn]; int mu[maxn ...

  3. 《大型网站系统与Java中间件实现》有感

    头一次只用了一周的时间就看完一本书<大型网站系统与Java中间件实现>,这本书是关于设计方面的,提到了服务框架,消息中间件,数据访问层,以及如何解决应用之间的调用,解耦,以及应用和存储之间 ...

  4. sqlite 使用 cte 及 递归的实现示例

    1.多级 cte 查询示例. with cte as ( select pageid from cm_bookpage ) , cte2 as ( as content from cte ) sele ...

  5. Boot Hill 布特山

    发售年份 1977 平台 街机 开发商 Midway 类型 射击 https://www.youtube.com/watch?v=yFVZhSCjo6w

  6. Taro 常用 API

    table th:first-of-type { width: 300px; } Taro 常用 API 说明 网址 Taro.getSystemInfoSync() 获取系统信息同步接口. http ...

  7. php获取数组最后一个值

    $array = array(1,2,3,4,5);

  8. 1、Nexus安装

    1.nexus 下载地址: https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.14.5-02-bundle.tar.g ...

  9. centos7系统优化定制

    #!/bin/bash #author junxi by #this script is only for CentOS 7.x #check the OS platform=`uname -i` i ...

  10. AllocateHWnd SetTimer API

    unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...