在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. 2.go的变量和常量

    go的变量和常量 GO的变量: 变量的声明:  先对变量进行声明,在对其赋值 var variableName type variableName = typeValue var number int ...

  2. ELK 起航

    ELK与我 我在2017年8月份第一次听说ELK并搭建了一次,当时看到KIBANA页面超级炫酷非常激动.现在已经过去了四个月了,现在的情况不像刚开始哪有无知了.现在是要应用到实际的项目中.首先说一下整 ...

  3. ACM山东工商 数据结构与算法 第3章 双向栈的操作

    #include <stdio.h>#include <stdlib.h> #define SIZE   20//1左 偶 typedef struct hold{ int s ...

  4. 上手d3js

    0---什么是d3js: d3js是一个开源的,基于对svg操作的数据可视化框架,简单的说他提供了数据提供了一系列的数据可视化工具,可以用他很方便的创造出关于svg的动画:svg动画具有可伸缩,不失真 ...

  5. 第一次博客作业(初识C++)

    Q1:学习<C++语言程序设计>课程之前,你知道什么是编程吗?谈谈上这门课之前你对编程的理解,以及你对自己编程能力的评估. A1:开始课程之前,我认为编程是这样的:用计算机的语言写一份流程 ...

  6. jQuery案例2

    $(this).index用来获取取到的所有元素的序号 省市联动 <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xh ...

  7. 谷歌chrome浏览器vue调试工具vue-devtools的安装

    先导 vue-devtools是一款基于chrome浏览器的插件,用于vue应用的调试,这款vue调试神器可以极大地提高我们的调试效率.帮助我们快速的调试开发vue应用. 第一步: 我们可以先从git ...

  8. maven+eclipse+jboss+oracle 12c+memcached+AngularJS

    Maven 参考梁总的: Eclipse Java EE IDE for Web Developers集成的Maven 3 指向自己安装的 Maven Maven下载.安装和配置(二) 在本地配置ma ...

  9. 分词工具Hanlp基于感知机的中文分词框架

     结构化感知机标注框架是一套利用感知机做序列标注任务,并且应用到中文分词.词性标注与命名实体识别这三个问题的完整在线学习框架,该框架利用1个算法解决3个问题,时自治同意的系统,同时三个任务顺序渐进,构 ...

  10. 精读《C++ primer》学习笔记(第一至三章)

    第一章: 重要知识点: 类型:一种类型不仅定义了数据元素的内容,还定义了这类数据上可以进行的运算:所以说类定义,实际上就是定义了一种数据类型: >>和<<运算符返回其左侧的运算 ...