Spring MVC 数据绑定(四)
public void registBean(String username,int age,double income,String[] interests,boolean marrid)
保证JSP页面上提交项和方法中对应参数同名。其中boolean的提交项的value是字符串true和false。
public void registBean(UserBean2 user)
public class UserBean2{
private List<AddressListBean> list= new ArrayList<AddressListBean>();
}
public void registBean(UserBean2 user)
JSP页面上提交项的命名:参数名[下标].属性名。
public class UserBean2{
private Set<AddressSetBean> set= new HashSet<AddressSetBean>();
public Classes(){
set.add(new AddressSetBean());
set.add(new AddressSetBean());
set.add(newAddressSetBean());
}
}
public class UserBean2{
private Map<String String> map= new HashMap<String String>();
}
public void registBean(UserBean2 user)
public class UserBean2{
private String name;
private int age;
private AddressBean address;
}
public void registBean(UserBean2 user)
JSP页面提交项命名:复合属性名.属性名
主要代码:
package com.cy.springannotation.entity; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set; public class UserBean2 {
private String username;
private int age;
private double income;
private String[] interests;
private boolean married;
//List不能单独使用,必须要绑定到某一个具体的Bean,并且要实例化ArrayList
private List<AddressListBean> list=new ArrayList<AddressListBean>();
//Set不能单独使用,必须要绑定到某一个具体的Bean,并且要实例化HashSet
private Set<AddressSetBean> set=new HashSet<AddressSetBean>();
//Map不能单独使用,必须要绑定到某一个具体的Bean,并且要实例化HashMap
private Map<String,AddressMapBean> map=new HashMap<String,AddressMapBean>();
//复合数据类型
private AddressBean address; public UserBean2(){
//set集合需要提前定义个数
for(int i=0;i<2;i++){
AddressSetBean asb=new AddressSetBean();
set.add(asb);
} }
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getIncome() {
return income;
}
public void setIncome(double income) {
this.income = income;
}
public String[] getInterests() {
return interests;
}
public void setInterests(String[] interests) {
this.interests = interests;
}
public boolean isMarried() {
return married;
}
public void setMarried(boolean married) {
this.married = married;
} public Map<String, AddressMapBean> getMap() {
return map;
}
public void setMap(Map<String, AddressMapBean> map) {
this.map = map;
} public Set<AddressSetBean> getSet() {
return set;
}
public void setSet(Set<AddressSetBean> set) {
this.set = set;
}
public List<AddressListBean> getList() {
return list;
}
public void setList(List<AddressListBean> list) {
this.list = list;
}
public AddressBean getAddress() {
return address;
}
public void setAddress(AddressBean address) {
this.address = address;
} }
页面设置“
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>注册页面</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<form action="regist.do" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td>收入:</td>
<td><input type="text" name="income"/></td>
</tr>
<tr>
<td>兴趣:</td>
<td><input type="checkbox" name="interests" value="看书"/>看书
<input type="checkbox" name="interests" value="上网"/>上网
<input type="checkbox" name="interests" value="唱歌"/>唱歌
<input type="checkbox" name="interests" value="运动"/>运动
<input type="checkbox" name="interests" value="散步"/>散步
</td>
</tr>
<tr>
<td>是否结婚:</td>
<td><input type="radio" name="married" value="true" checked="checked"/>是
<input type="radio" name="married" value="false" />否
</td>
</tr>
<tr>
<td>list地址1:</td>
<td><input type="text" name="list[0].addressListName"/></td>
</tr>
<tr>
<td>list地址2:</td>
<td><input type="text" name="list[1].addressListName"/></td>
</tr> <tr>
<td>set地址1:</td>
<td><input type="text" name="set[0].addressSetName"/></td>
</tr>
<tr>
<td>set地址2:</td>
<td><input type="text" name="set[1].addressSetName"/></td>
</tr>
<tr>
<td>map地址1:</td>
<td><input type="text" name="map['a0'].addressMapName"/></td>
</tr>
<tr>
<td>map地址2:</td>
<td><input type="text" name="map['a1'].addressMapName"/></td>
</tr>
<tr>
<td>地址:</td>
<td><input type="text" name="address.addressName"/></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="注册"/> </td>
</tr>
</table>
</form>
</body>
</html>
业务控制器:
package com.cy.springannotation.controller; import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.cy.springannotation.entity.AddressListBean;
import com.cy.springannotation.entity.AddressSetBean;
import com.cy.springannotation.entity.UserBean2; @Controller
public class RegistController {
private Logger log = Logger.getLogger(this.getClass()); /* @RequestMapping("/regist.do")
public String regist(String username,int age,double income,String[] interests,boolean married){
log.info("username="+username);
log.info("age="+age);
log.info("income="+income);
log.info("interests="+interests.length);
log.info("married="+married);
for (String string : interests) {
log.info("兴趣有:"+string);
}
return "index"; }
*/
//简单对象类型
@RequestMapping("/regist.do")
public String registBean(UserBean2 user){
log.info("username:"+user.getUsername());
log.info("age:"+user.getAge());
log.info("income:"+user.getIncome());
log.info("interests.length:"+user.getInterests().length);
log.info("married:"+user.isMarried());
log.info("addressList.size():"+user.getList().size());
log.info("addressSet.size():"+user.getSet().size());
log.info("addressMap.size():"+user.getMap().size());
log.info("address:"+user.getAddress().getAddressName()); for (String string : user.getInterests()) {
log.info("具体的兴趣有=========" + string);
} for(AddressListBean add:user.getList()){
log.info("具体的list地址有=========" + add.getAddressListName());
} for(AddressSetBean asetb:user.getSet()){
log.info("具体的set地址有=========" + asetb.getAddressSetName());
} for(int i=0;i<2;i++){
log.info("具体的map地址有=========" + user.getMap().get("a"+i).getAddressMapName());
} return "index";
} }
控制台显示:
数据绑定over。
Spring MVC 数据绑定(四)的更多相关文章
- Spring MVC—数据绑定机制,数据转换,数据格式化配置,数据校验
Spring MVC数据绑定机制 数据转换 Spring MVC处理JSON 数据格式化配置使用 数据校验 数据校验 Spring MVC数据绑定机制 Spring MVC解析JSON格式的数据: 步 ...
- spring mvc 数据绑定
1.spring mvc 默认提供的数据绑定类 private List<HandlerMethodArgumentResolver> getDefaultArgumentResolver ...
- Spring MVC 数据绑定流程分析
1. 数据绑定流程原理★ ① Spring MVC 主框架将 ServletRequest 对象及目标方法的入参实例传递给 WebDataBinderFactory 实例,以创建 Data ...
- Spring MVC数据绑定(一)
1.数据绑定介绍 在执行程序时,Spring MVC会根据客户端请求参数的不同,将请求消息中的信息以一定的方式转换并绑定到控制器类的方法参数中.这种将请求消息数据与后台方法参数建立连接的过程就是Spr ...
- Spring MVC 梳理 - 四种HandlerMapping
总结 (1)SpringMVC导入jar包,配置web.xml 这些都是基础的操作. (2)四种HandlerMapping:DefaultAnnotationHandlerMapping;Simpl ...
- spring mvc 数据绑定总结
spring mvc 做web开发时,经常会不知道如何合适绑定页面数据.用惯struts2的朋友更认为spring mvc 绑定数据不如struts2方便(本人最开始也是这么认为),经过一段时间的应用 ...
- Spring MVC数据绑定大全 .
刚开始用spring mvc 做web开发时,经常会不知道如何合适绑定页面数据.用惯struts2的朋友更认为spring mvc 绑定数据不如struts2方便(本人最开始也是这么认为),经过一段时 ...
- Spring MVC(十四)--SpringMVC验证表单
在Spring MVC中提供了验证器可以进行服务端校验,所有的验证都必须先注册校验器,不过校验器也是Spring MVC自动加载的,在使用Spring MVC校验器之前首先要下载相关的jar包,下面是 ...
- Spring MVC 数据绑定 (四)
完整的项目案例: springmvc.zip 目录 实例 项目结构路径: 一.配置web.xml <?xml version="1.0" encoding="UTF ...
随机推荐
- 在opencv中实现中文输出
http://pan.baidu.com/s/1hrQTWDe 已经成功 ; 来自为知笔记(Wiz)
- AJAX文档
AJAX 文档 AJAX开发简略.................................................................................... ...
- javase建议学习路线
javase建议学习路线:1.环境的搭建2.基本变量类型3.流程控制4.数组5.集合6.IO7.异常8.线程9.网络编程
- mysql 修改数据库的列
alter table tableName oldcolumn newcolumn datatype;
- 批量修改文件夹及文件用户权限和用户组权限 centos
chown -R www * 批量修改目录下所有文件,用户为www chown -R :www * 批量修改目录下所有文件,用户组为www
- 异常详细信息: System.Web.Hosting.HostingEnvironmentException: 访问 IIS 元数据库失败 解决方法
访问IIS元数据库失败 同理,给操作系统的新建用户赋予IIS操作权限同样可以采用该命令来处理 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错 ...
- SQL 汉字转换成拼音首字母 首字母查
-- ============================================= -- 功能:汉字转换成拼音首字母 首字母查 -- ========================== ...
- SSMS Tools Pack
http://www.ssmstoolspack.com/Features Format SQL 大小写切换,只切换关键字 http://www.ssmstoolspack.com/Features? ...
- 并行parallel和并发concurrent的区别
http://stackoverflow.com/questions/1050222/concurrency-vs-parallelism-what-is-the-difference Concurr ...
- Creating List Item in Oracle D2k
Special Tips for List Items in Oracle D2k In this section, I shall discuss some special tips and tec ...