@InitBinder的使用
来源:互联网 发布:2017网络作家收入 编辑:程序博客网 时间:2024/06/05 19:34
转载地址:
http://my.oschina.net/uniquejava/blog/85727
spring mvc的表单类型转换太强大了,目前用到了两个简单的,
一个是将表单中的file自动映射成byte[],这样文件上传(如果使用blob)就无需写任何代码了。
另一个是将表单中的yyyy-MM-dd格式映射成java.util.Date,
假设User.java中有如下这两种特殊的属性:
1
public
class
User
implements
Serializable{
2
private
Date birth;
3
private
byte
[] icon;
4
}
注册这两种属性编辑器只需在Controller中定义如下这样一个initBinder
方法:
01
@Controller
(
"userController"
)
02
@RequestMapping
(value =
"/user"
)
03
public
class
UserController {
04
@RequestMapping
(value =
"create"
, method = RequestMethod.POST)
05
public
String create(
@ModelAttribute
(
"user"
) User user,
06
RedirectAttributes redirectAttributes) {
07
userService.createUser(user);
08
redirectAttributes.addFlashAttribute(
"message"
,
"create success!"
);
09
10
return
SUCCESS;
11
}
12
13
@InitBinder
14
protected
void
initBinder(
15
WebDataBinder binder)
throws
ServletException {
16
binder.registerCustomEditor(
byte
[].
class
,
17
new
ByteArrayMultipartFileEditor());
18
19
SimpleDateFormat dateFormat =
new
SimpleDateFormat(
"yyyy-MM-dd"
);
20
dateFormat.setLenient(
false
);
21
binder.registerCustomEditor(Date.
class
,
new
CustomDateEditor(dateFormat,
false
));
22
}
23
}
ByteArrayMultipartFileEditor和CustomDateEditor都是spring直接提供的。可以参考这两个类的源码,
高级的自定义的还没用过,等用到的时候再补充到这里(2012-11-04补充)
今天终于用到了自定义的Editor,我现在有一个User对象,它有一个Set<Role> roles集合。
1
public
class
User
implements
Serializable{
2
public
Set<Role> roles =
new
HashSet<Role>();
1
public
class
Role
implements
Serializable {
2
private
Long id;
//
3
private
String name;
//
UserController如下
1
@RequestMapping
(value =
"create"
, method = RequestMethod.GET)
2
public
String createForm(ModelMap model) {
3
model.addAttribute(
"roleList"
, roleService.findAllRoles());
4
User user =
new
User();
5
model.addAttribute(user);
6
return
"user/user_new"
;
7
}
1
<
div
class
=
"control-group"
>
2
<
label
class
=
"control-label"
for
=
"roles"
>角色:</
label
>
3
<
div
class
=
"controls"
>
4
<
sf:checkboxes
path
=
"roles"
items
=
"${roleList }"
itemValue
=
"id"
itemLabel
=
"name"
/>
5
</
div
>
6
</
div
>
可以像这样定义RoleEditor.java
01
public
class
RoleEditor
extends
PropertyEditorSupport {
02
private
RoleService roleService;
03
04
public
RoleEditor(RoleService roleService) {
05
this
.roleService = roleService;
06
}
07
08
@Override
09
public
void
setAsText(String text)
throws
IllegalArgumentException {
10
if
(text !=
null
) {
11
Role role = roleService.findRoleById(Long.valueOf(text));
12
setValue(role);
13
}
else
{
14
setValue(
null
);
15
}
16
}
17
}
1
@InitBinder
2
protected
void
initBinder(
3
WebDataBinder binder)
throws
ServletException {
4
//@see http://forum.springsource.org/showthread.php?59612-Service-injection-amp-PropertyEditor
5
binder.registerCustomEditor(Role.
class
,
new
RoleEditor(roleService));
6
}
1
@RequestMapping
(value =
"create"
, method = RequestMethod.POST)
2
public
String create(
@ModelAttribute
(
"user"
) User user,
3
RedirectAttributes redirectAttributes) {
4
userService.createUser(user);
5
redirectAttributes.addFlashAttribute(
"message"
,
"create success!"
);
6
7
return
SUCCESS;
8
}
值得注意的是,你必须要覆写Role的equals和hashCode方法,不然当你进入修改页面时,user的role属性不会自动的check上。
这里有人提相同的问题:
http://stackoverflow.com/questions/7421346/spring-binding-listobject-to-formcheckboxes
还有这里
http://stackoverflow.com/questions/8700339/spring-mvc-usage-of-formcheckbox-to-bind-data
参考:
http://www.mkyong.com/spring-mvc/spring-mvc-failed-to-convert-property-value-in-file-upload-form/
http://linkedjava.blogspot.com/2011/06/spring-controller-with-date-object.html(需要FQ)
http://forum.springsource.org/showthread.php?59612-Service-injection-amp-PropertyEditor
- @InitBinder的使用
- @InitBinder注解的使用
- Spring中@initBinder的使用
- spring中@InitBinder和WebBindingInitializer的使用
- springMVC @initBinder 使用
- @InitBinder注解使用
- 一.spring中@InitBinder和WebBindingInitializer的使用
- @InitBinder的作用
- @InitBinder
- @initBinder
- @initBinder
- @InitBinder
- spring mvc initBinder 使用笔记
- springMVC类型转换,@initBinder使用
- Spring MVC使用InitBinder验证
- 使用@InitBinder进行数据转换
- springmvc中initbinder的作用
- springMVC里面的注解@InitBinder
- poj-2762-Going from u to v or from v to u?-tarjan算法求缩点+算是不是一字链
- 我和我的输入提示--格式化输入文件
- Ubuntu安装单机1.2.1hadoop与伪分布式模式进行WordCount实验
- hdu 1558 Segment set 线段相交+并查集
- Hadoop YARN配置参数剖析(4)—Fair Scheduler相关参数
- @InitBinder的使用
- 继承System.Web.UI.Page的页面基类
- 主设备号、次设备号
- 英语学习利器,后悔后知后觉了--------------------韦氏三宝
- POJ 2236Wireless Network
- hdu 1107 一道很霸气的模拟题目
- 端到端
- 黑马程序员_基础加强第一天——Java反射机制
- pthread_setspecific出现coredump