• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >JSP > SpringMVC 数据绑定实例详解

SpringMVC 数据绑定实例详解

作者:Senton 字体:[增加 减小] 来源:互联网 时间:2017-05-11

Senton通过本文主要向大家介绍了springmvc注解详解,springmvc配置详解,springmvc原理详解,springmvc详解,springmvc框架详解等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

 SpringMVC 数据绑定

查看spring源码可以看出spring支持转换的数据类型:

org.springframework.beans.PropertyEditorRegistrySupport:

/** 
 * Actually register the default editors for this registry instance. 
 */ 
private void createDefaultEditors() { 
  this.defaultEditors = new HashMap<Class, PropertyEditor>(64); 
 
  // Simple editors, without parameterization capabilities. 
  // The JDK does not contain a default editor for any of these target types. 
  this.defaultEditors.put(Charset.class, new CharsetEditor()); 
  this.defaultEditors.put(Class.class, new ClassEditor()); 
  this.defaultEditors.put(Class[].class, new ClassArrayEditor()); 
  this.defaultEditors.put(Currency.class, new CurrencyEditor()); 
  this.defaultEditors.put(File.class, new FileEditor()); 
  this.defaultEditors.put(InputStream.class, new InputStreamEditor()); 
  this.defaultEditors.put(InputSource.class, new InputSourceEditor()); 
  this.defaultEditors.put(Locale.class, new LocaleEditor()); 
  this.defaultEditors.put(Pattern.class, new PatternEditor()); 
  this.defaultEditors.put(Properties.class, new PropertiesEditor()); 
  this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor()); 
  this.defaultEditors.put(TimeZone.class, new TimeZoneEditor()); 
  this.defaultEditors.put(URI.class, new URIEditor()); 
  this.defaultEditors.put(URL.class, new URLEditor()); 
  this.defaultEditors.put(UUID.class, new UUIDEditor()); 
 
  // Default instances of collection editors. 
  // Can be overridden by registering custom instances of those as custom editors. 
  this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class)); 
  this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class)); 
  this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class)); 
  this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class)); 
  this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class)); 
 
  // Default editors for primitive arrays. 
  this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor()); 
  this.defaultEditors.put(char[].class, new CharArrayPropertyEditor()); 
 
  // The JDK does not contain a default editor for char! 
  this.defaultEditors.put(char.class, new CharacterEditor(false)); 
  this.defaultEditors.put(Character.class, new CharacterEditor(true)); 
 
  // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor. 
  this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false)); 
  this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true)); 
 
  // The JDK does not contain default editors for number wrapper types! 
  // Override JDK primitive number editors with our own CustomNumberEditor. 
  this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false)); 
  this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true)); 
  this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false)); 
  this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true)); 
  this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false)); 
  this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true)); 
  this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false)); 
  this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true)); 
  this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false)); 
  this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true)); 
  this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false)); 
  this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true)); 
  this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true)); 
  this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true)); 
 
  // Only register config value editors if explicitly requested. 
  if (this.configValueEditorsActive) { 
    StringArrayPropertyEditor sae = new StringArrayPropertyEditor(); 
    this.defaultEditors.put(String[].class, sae); 
    this.defaultEditors.put(short[].class, sae); 
    this.defaultEditors.put(int[].class, sae); 
    this.defaultEditors.put(long[].class, sae); 
  } 
} 
</div>

下面挑选一些常用的数据类型,举例说明它们的绑定方式

1. 基本数据类型(以int为例,其他类似):

    Controller代码:

@RequestMapping("test.do") 
public void test(int num) { 
   
} 
</div>

    JSP表单代码:

<form action="test.do" method="post"> 
  <input name="num" value="10" type="text"/> 
  ...... 
</form> 
</div>

表单中input的name值和Controller的参数变量名保持一致,就能完成基本数据类型的数据绑定,如果不一致可以使用@RequestParam标注实现。值得一提的是,如果Controller方法参数中定义的是基本数据类型,但是从jsp提交过来的数据为null或者""的话,会出现数据转换的异常。也就是说,必须保证表单传递过来的数据不能为null或"",所以,在开发过程中,对可能为空的数据,最好将参数数据类型定义成包装类型,具体参见下面的第二条。

2. 包装类型(以Integer为例,其他类似):

    Controller代码:

@RequestMapping("test.do") 
public void test(Integer num) { 
   
} 
</div>

   JSP表单代码:

<form action="test.do" method="post"> 
  <input name="num" value="10" type="text"/> 
  ...... 
</form> 
</div>

和基本数据类型基本一样,不同之处在于,JSP表单传递过来的数据可以为null或"",以上面代码为例,如果jsp中num为""或者表单中无num这个input,那么,Controller方法参数中的num值则为null。

3. 自定义对象类型:

    Model代码:

public class User { 
 
  private String firstName; 
 
  private String lastName; 
 
  public String getFirstName() { 
    return firstName; 
  } 
 
  public void setFirstName(String firstName) { 
    this.firstName = firstName; 
  } 
 
  public String getLastName() { 
    return lastName; 
  } 
 
  public void setLastName(String lastName) { 
    this.lastName = lastName; 
  } 
 
} 
</div>

    Controller代码:

@RequestMapping("test.do") 
public void test(User user) { 
   
} 
</div>

    JSP表单代码:

<form action="test.do" method="post"> 
  <input name="firstName" value="张" type="text"/> 
  <input name="lastName" value="三" type="text"/> 
  ...... 
</form> 
</div>

非常简单,只需将对象的属性名和input的name值一一对应即可。

4. 自定义复合对象类型:

    Model代码:

public class ContactInfo { 
 
  private String tel; 
 
  private String address; 
 
  public String getTel() { 
    return tel; 
  } 
 
  public void setTel(String tel) { 
    this.tel = tel; 
  } 
 
  public String getAddress() { 
    return address; 
  } 
 
  pu



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • SpringMVC 数据绑定实例详解

相关文章

  • 2017-05-11struts2中action实现ModelDriven后无法返回json的解决方法
  • 2017-05-11WIN2000+PHP+MYSQL+TOMCAT+JSP完全整合安装手册
  • 2017-05-11jsp重定向地址栏不改变的实例
  • 2017-05-11JSP程序运行原理、文档结构及简单输入输出实例分析
  • 2017-05-11JSP中常用的JSTL fmt(format格式化)标签用法整理
  • 2017-05-11jsp页面传参乱码的解决方法
  • 2017-05-11JSP父页面传参数到子页面及接收示例
  • 2017-05-11一个jsp+AJAX评论系统第1/2页
  • 2017-05-11JSP教程(一)
  • 2017-05-11JSP开发入门(一)--安装好你的机器来使用JSP

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • JSP基于JDBC的数据库连接类实例
    • 下载完成后页面不自动关闭的方法
    • Jquery、Ajax、Struts2完成定时刷新的方法
    • Spring MVC的文件下载实例详解
    • JSP Servelet 数据源连接池的配置
    • 使用JSP + JAVABEAN + XML 开发的一个例子
    • 用缓冲技术提高JSP应用的性能和稳定性
    • jsp利用POI生成Excel并在页面中导出的示例
    • Spring在web.xml中的配置详细介绍
    • 在linux上建jsp環境

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有