@InitBinder

1 SpringMvc @InitBinder 表单多对象精准绑定接收

https://blog.csdn.net/xsf1840/article/details/73556633  说的很好

提交连个对象的处理

<form action="/test/test" method="post">  <input type="text" name="user.id" value="huo_user_id">  <input type="text" name="user.name" value="huo_user_name">  <input type="text" name="addr.id" value="huo_addr_id">  <input type="text" name="addr.name" value="huo_addr_name">  <input type="submit" value="提交">
</form>  
@Controller
@RequestMapping("/test")
public class TestController {
// 绑定变量名字和属性,参数封装进类  @InitBinder("user")  public void initBinderUser(WebDataBinder binder) {  binder.setFieldDefaultPrefix("user.");  }  // 绑定变量名字和属性,参数封装进类  @InitBinder("addr")  public void initBinderAddr(WebDataBinder binder) {  binder.setFieldDefaultPrefix("addr.");  }  @RequestMapping("/test")  @ResponseBody  public Map<String,Object> test(HttpServletRequest request,@ModelAttribute("user") User user,@ModelAttribute("addr") Addr addr){  Map<String,Object> map=new HashMap<String,Object>();  map.put("user", user);  map.put("addr", addr);  return map;  }  

2 时间类型的转换

在使用SpringMVC的时候,经常会遇到表单中的日期字符串和JavaBean的Date类型的转换,而SpringMVC默认不支持这个格式的转换,所以需要手动配置,自定义数据的绑定才能解决这个问题。

Controller继承的BaseController中

 /*** 初始化绑定<br/>* @param request http请求实例* @param binder 绑定实例* @throws Exception* */@InitBinder  public void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {   binder.registerCustomEditor(Date.class, new DateEditor());}
/*** <p><strong>类名: </strong></p>DateEditor <br/>* <p><strong>功能说明: </strong></p>日期数据自动转换类. <br/>* @since    JDK 1.8     */
public class DateEditor extends PropertyEditorSupport {public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  public SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  public void setAsText(String text){setValue(parseString(text));}private Object parseString(String text){Date date = null;if (null != text && !"".equals(text)) {try {dateTimeFormat.setLenient(false);date = dateTimeFormat.parse(text);} catch (ParseException e) {//e.printStackTrace();}if (date == null) {try {dateFormat.setLenient(false);date = dateFormat.parse(text);} catch (ParseException e) {//e.printStackTrace();}}}return date;}
}

3 是否绑定某个属性 或者只绑定某个属性

@InitBinder
private void initBinder(WebDataBinder binder){  //由表单到JavaBean赋值过程中哪一个值不进行赋值  binder.setDisallowedFields("lastName");
}  

Spring @InitBinder注解相关推荐

  1. spring @InitBinder注解使用

    今天在xxl-job源码上看到这个,发现我从没用过,特意了解一下. 如果controller有任何用@InitBinder注释的公共方法,那么这些方法将在处理每个请求之前由容器调用,并传递给WebDa ...

  2. spring 基于注解的控制器配置

    http://ttaale.iteye.com/blog/787586 spring 基于注解的控制器配置 博客分类: spring SpringBeanServletMVCWeb 13.12. 基于 ...

  3. 【整理】Spring 常用注解!千万不要错过!

    来自:徐宝 | 责编:乐乐 链接:my.oschina.net/lixingsikao/blog/3218281 干货前奏 随着技术的更新迭代Java5.0开始支持注解.作为Java中的头号领军 sp ...

  4. Java 必须掌握的 12 种 Spring 常用注解

    转载自  Java 必须掌握的 12 种 Spring 常用注解 1.声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repo ...

  5. Date类型接收空字符串(@InitBinder注解实现)

    当后端实体类中的属性为Date类型的数据接收前端传过来的数据为空字符串时,可能会报错(Failed to convert property value of type 'java.lang.Strin ...

  6. SpringMVC中的@InitBinder注解【记录】

    一.Spring请求参数绑定流程: 1.请求参数绑定流程: 我们在开发的时候,经常会从html,jsp中将请求参数通过request对象传递到后台,可是经常会遇到这么一种情况,那就是传过来的数据到后台 ...

  7. @InitBinder注解绑定表单数据

    在SpringMVC框架的项目中,bean中定义了Date,double等类型,如果没有做任何处理的话,日期以及double都无法绑定.      比如经常会遇到页面表单中某些数据类型是Date.In ...

  8. @InitBinder 注解的使用

    @InitBinder注解的使用 要在一张表单中提交多个对象,并且每个对象的属性可能名称相同,在Controller中如何绑定? public class User implements Serial ...

  9. 【Spring】spring基于注解的声明式事务控制

    结构 domin package com.itheima.domain;import java.io.Serializable;public class Account implements Seri ...

最新文章

  1. 生成N个不相等的随机数
  2. Service Mesh 从“趋势”走向“无聊”
  3. LeetCode175 组合两个表
  4. MySQL数据库:查看数据库性能常用命令
  5. wxWidgets:拖放样本
  6. c++整理程序 dev_C编程从入门到实践:C语言开发工具详解(2)
  7. docker 安全性问题
  8. 2021安徽高考成绩及录取结婚查询,2020安徽高考录取结果查询时间及通知书发放时间...
  9. ACM 学习笔记(五) 动态规划
  10. 智能电能计量管理系统
  11. 使用dll查看器dll文件中的内容
  12. pytorch实现resnet网络结构
  13. JUnit学习笔记6---用stub进行粗粒度测试
  14. XTU oj 1112 三点共线
  15. ADS学习:统计分析——蒙特卡洛分析、良率分析
  16. mysql如何查看事务日记_详解 Mysql 事务和Mysql 日志
  17. zoj 2343 Robbers 【贪心】
  18. Learning Shape Representations for Clothing Variations in Person Re-Identification
  19. 【Java基础快速入门】概述及开发环境搭建
  20. Golang:实现断点续传(http单线程下载文件)

热门文章

  1. 传奇列表获取失败与登录器太老怎么解决
  2. 小知识·CMake基本指令
  3. 大前端 - react- 服务端渲染 - Gatsby
  4. 如何零成本实现微信公众号自助查券返利机器人(五)
  5. SpringBoot+MangoDB查询操作(MongoTemplate)总结
  6. Linux下异步IO(libaio)的使用以及性能
  7. 根据结构体数组中某一数据项对结构体数组排序
  8. 条件自信息跟互信息区别简述
  9. (c语言)通俗易懂的冒泡代码思路
  10. Gradle 2.0 用户指南翻译——第五十章. 依赖管理