@InitBinder
类型转换:
请求url: http://localhost:8080/SSHDemo2/stu/pro?s=zk,19
传入参数 s=zk,19 转换为Student
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "toString - name="+name+";age="+age;
}
}
public class StudentEditor extends PropertyEditorSupport{
@Override
public void setAsText(String text) throws IllegalArgumentException {
Student s = new Student();
String[] attrs = text.split(",");
if(attrs.length==1){
s.setName(attrs[0]);
}
if(attrs.length==2){
s.setName(attrs[0]);
s.setAge(Integer.parseInt(attrs[1]));
}
System.out.println("setAsText");
setValue(s);
}
@Override
public String getAsText() {
System.out.println("getAsText");
return getValue().toString();
}
}
@Controller
@RequestMapping("/stu")
public class StudentController { @RequestMapping("/pro")
public String stuPropertyEditor(@RequestParam("s") Student s){
System.out.println("name="+s.getName()+";age="+s.getAge());
return "success";
} @InitBinder
public void initBinder(WebDataBinder binder){
System.out.println("init..");
binder.registerCustomEditor(Student.class, new StudentEditor());
} }
此时也可以不要@InitBinder,因为bean为Student则StudentEditor会默认为它的属性编辑器
@InitBinder的更多相关文章
- 类型转换器(InitBinder 初始化绑定器)
单日期格式 导入jar包 创建FirstController.java @Controller public class FirstController { /** * @param binder * ...
- Spring mvc @initBinder 类型转化器的使用
一.单日期格式 因为是用注解完完成的后台访问,所以必须在大配置中配置包扫描器: 1.applicactionContext.xml <?xml version="1.0" e ...
- spring类型自动转换——@InitBinder和Converter
spring有2种类型转换器,一种是propertyEditor,一种是Converter.虽然都是类型转换,但是还是有细微差别. 所以这里以一个例子的形式来分析一下这2种类型转换的使用场景和差别. ...
- SpringMVC中利用@InitBinder来对页面数据进行解析绑定
同步发布:http://www.yuanrengu.com/index.php/springmvc-user-initbinder.html 在使用SpingMVC框架的项目中,经常会遇到页面某些数据 ...
- spring mvc使用@InitBinder 标签对表单数据绑定
在SpringMVC中,bean中定义了Date,double等类型,如果没有做任何处理的话,日期以及double都无法绑定. 解决的办法就是使用spring mvc提供的@InitBinder标签 ...
- SpringMVC使用@PathVariable,@RequestBody,@ResponseBody,@RequestParam,@InitBinder
@Pathvariable public ResponseEntity<String> ordersBack( @PathVariable String reqKey, ...
- [Spring MVC] - InitBinder验证
Spring MVC使用InitBinder验证: 使用InitBinder做验证的情况一般会在此Controller中提交的数据需要有一些是业务性质的,也即比较复杂的验证情况下才会使用.大部份简单的 ...
- springMVC注解@initbinder日期类型的属性自动转换
在实际操作中经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换, 而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能. 一 ...
- SpringMVC注解@initbinder解决类型转换问题
在使用SpringMVC的时候,经常会遇到表单中的日期字符串和JavaBean的Date类型的转换,而SpringMVC默认不支持这个格式的转换,所以需要手动配置,自定义数据的绑定才能解决这个问题.在 ...
随机推荐
- Jquery ajax提交表单几种方法详解
[导读] 在jquery中ajax提交表单有post与get方式,在使用get方式时我们可以直接使用ajax 序列化表单$( 表单ID) serialize();就行了,下面我来介绍两个提交表单数据的 ...
- C#利用Windows API 实现关机、注销、重启等操作
using System; using System.Text; using System.Diagnostics; using System.Runtime.InteropServices; nam ...
- Java ArrayList操作
import java.util.ArrayList; import java.util.List; import java.util.Iterator; public class Study { p ...
- 【ntp】centos7下ntp服务器设置
安装ntp #检查服务是否安装 rpm -q ntp #安装ntp服务器 yum -y install ntp 修改配置文件:/etc/ntp.conf 内容如下: restrict default ...
- Bind9用view配主从
We use two Bind server to realize view, master, slave----------------------------------------------- ...
- Mysql讲解数据库并发控制知识
1.下载Mysql并安装,我喜欢不用安装的zip版,cd到bin目录下,先修改下mysql的密码. mysqladmin -u root -p password mysql ,第一次运行并修改mysq ...
- (C/C++) memset
C语言: memset extern void *memset(void *buffer,int c,int count); #include <string.h> 功能:把b ...
- [Android Exception A] – 1-The following classes could not be instantiated
http://stackoverflow.com/questions/26575815/the-following-classes-could-not-be-instantiated-android- ...
- codeforces 323A. Black-and-White Cube 构造
输入n 1 <= n <= 100 有一个n * n * n 的立方体,由n ^ 3 个1 * 1 * 1 的单位立方体构成 要用white 和 black 2种颜色来染这n ^ 3个立方 ...
- HDU 2196 Computer 树形DP 经典题
给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...