在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. git push 不想把本地某个目录下文件上传的办法

  2. 使用DHCP动态管理主机地址

  3. WPF-MVVM-ICommand接口实现

    一 接口分析MVVM框架的目的就是让视图和业务逻辑分离,各干各的.那么怎样实现分离呢,精髓就是绑定ICommand.先看一下ICommand接口的定义: // // 摘要: // 定义一个命令. [T ...

  4. MUD 多人地下城

    发售年份 1980 平台 多平台 开发商 Roy Trubshaw, Richard Battle 类型 冒险 https://www.youtube.com/watch?v=338WE8O2-KA

  5. tab页的使用方法

    css代码: #main{ margin:0px; width:100%; height:540px; background:url(m.jpg) no-repeat; background-size ...

  6. 12 个 JS 技巧

    1. 过滤唯一值 ES6 引入了 Set 对象和延展(spread)语法…,我们可以用它们来创建一个只包含唯一值的数组. 复制代码     const array = [1, 1, 2, 3, 5, ...

  7. Oracle数据csv导入

    打开工具,在tool下面有个Text Importer 先选择Data from textfile选项卡 然后选择 Open data file ,打开要导入的文件 1\ 2\ 再先选择Data to ...

  8. 捕获数据中的某个序列---verilog

    捕获数据中的某个序列---verilog 状态变化图 先是检测序列,每当接收到cmp_equal信号时跳转到下一个状态,等待另外一个cmp_equal信号到来. 代码: always @ * case ...

  9. python3-基础1

    eval()  --- 返回表达式计算结果 实际上就是把括号中的命令提取出来执行一遍. eval("print('ok')") ok 可变类型: 在ID不变的情况下,value可变 ...

  10. php.ini文件下载

    该php.ini文件为修改版,完美运行,存于360云盘.