spring mvc的表单类型转换(custom property editor)
来源:互联网 发布:js获取div中的ul标签 编辑:程序博客网 时间:2024/06/06 11:20
spring mvc的表单类型转换太强大了,目前用到了两个简单的,
一个是将表单中的file自动映射成byte[],这样文件上传(如果使用blob)就无需写任何代码了。
另一个是将表单中的yyyy-MM-dd格式映射成java.util.Date,
假设User.java中有如下这两种特殊的属性:
public class User implements Serializable{ private Date birth; private byte[] icon;}```注册这两种属性编辑器只需在Controller中定义如下这样一个initBinder方法:<div class="se-preview-section-delimiter"></div>
这里写代码片
“`
@Controller("userController")@RequestMapping(value = "/user")public class UserController { @RequestMapping(value = "create", method = RequestMethod.POST) public String create(@ModelAttribute("user") User user, RedirectAttributes redirectAttributes) { userService.createUser(user); redirectAttributes.addFlashAttribute("message", "create success!"); return SUCCESS; } @InitBinder protected void initBinder( WebDataBinder binder) throws ServletException { binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); }}
ByteArrayMultipartFileEditor和CustomDateEditor都是spring直接提供的。
自定义的:
public class User implements Serializable{ public Set<Role> roles = new HashSet<Role>(); }
public class Role implements Serializable { private Long id; private String name;
UserController如下:
@RequestMapping(value = "create", method = RequestMethod.GET)public String createForm(ModelMap model) { model.addAttribute("roleList", roleService.findAllRoles()); User user = new User(); model.addAttribute(user); return "user/user_new";}
public class RoleEditor extends PropertyEditorSupport {2 private RoleService roleService; public RoleEditor(RoleService roleService) { this.roleService = roleService; } @Override public void setAsText(String text) throws IllegalArgumentException { if (text != null) { Role role = roleService.findRoleById(Long.valueOf(text)); setValue(role); } else { setValue(null); } }}
并在UserController中的initBinder方法中注册该编辑器
@InitBinderprotected void initBinder( WebDataBinder binder) throws ServletException { //@see http://forum.springsource.org/showthread.php?59612-Service-injection-amp-PropertyEditor binder.registerCustomEditor(Role.class, new RoleEditor(roleService));}
这时在UserController的create方法中取得的User对象就是已经绑定了roles的了
@RequestMapping(value = "create", method = RequestMethod.POST)public String create(@ModelAttribute("user") User user, RedirectAttributes redirectAttributes) { userService.createUser(user); redirectAttributes.addFlashAttribute("message", "create success!"); return SUCCESS;}
值得注意的是,你必须要覆写Role的equals和hashCode方法,不然当你进入修改页面时,user的role属性不会自动的check上。
0 0
- spring mvc的表单类型转换(custom property editor)
- Spring MVC中基于自定义Editor的表单数据处理技巧
- spring mvc的类型转换
- spring mvc 类型转换
- Spring MVC的表单
- spring mvc中类型转换
- Spring mvc参数类型转换
- spring mvc开发接收日期字段表单提交,自动转换成Date类型报错,解决办法
- spring mvc开发接收日期字段表单提交,自动转换成Date类型为什么报错?
- spring mvc表单复杂类型数据绑定
- 关于Spring中的property editor及converter的一些资料
- Spring MVC 的表单标签
- spring mvc int 类型转换错误
- Spring MVC JSON自定义类型转换
- Spring MVC JSON自定义类型转换
- 让spring mvc controller转换date类型
- Spring MVC JSON自定义类型转换
- Spring MVC 之类型转换 Converter
- 2017年签到
- 用zookeeper实现分布式session
- 一天一篇文章吧总结一天也总结自我
- 讯为itop4412开发板触摸屏调试
- Java 链接MYSQL 显示数据所有记录
- spring mvc的表单类型转换(custom property editor)
- 区块链开发(一)搭建基于以太坊的私有链环境
- 线程池 ExecutorService 详细介绍以及注意点区别
- Git——创建版本库
- Android 7.0对比Android6.0新特性
- JavaScript里面的prototype是什么意思
- 几种流行Webservice框架性能对比
- 解决在Fragment中getActivity()为空问题
- linux关闭防火墙