How to bind and validate in MultiActionController
来源:互联网 发布:手机淘宝优惠券怎么用? 编辑:程序博客网 时间:2024/05/20 11:24
In Spring framework, it provides several controller in MVC model. Personaly, I like the MultiActionController best. And I do not like SimpleFormController because it only one "onSubmit" method. For WEB page, I do not think any page just have one button, i.e: "Save" or "Update". However, SimpleFormController have strong validator ability. But I still hate lots of configure in SimpleFormController.
In Spring MultiActionController, it already provides bind method, however, this method just throws Exception when it find the invalidate error message. To catch this error, we need write a new method to override it.
The goal is to use both MultiActionController and SimpleFormController
Solution: bind and validate in MultiActionController.
In XML, you can write your MultiActionController as normal define.
Now we need bind the form and validator it.
I will use a save action as example:
First create a bindObject method in your BaseContoller (extends MultiActionController)
protected BindException bindObject(HttpServletRequest request,
Object command, Validator validator) throws Exception {
preBind(request, command);
ServletRequestDataBinder binder = createBinder(request, command);
binder.bind(request);
BindException errors = new BindException(command,
getCommandName(command));
if (validator.supports(command.getClass())) {
ValidationUtils.invokeValidator(validator, command, errors);
}
return errors;
}
Now in the save action, you can use this method as normal.
public ModelAndView save(HttpServletRequest request,
HttpServletResponse response, PhoneInfo command) throws Exception {
ModelAndView addPhoneView = new ModelAndView(LIST_VIEW, "phones",
phones);
addPhoneView.addObject("phoneInfo", command);
// add validator and call bindobject to get the result
BindException errors = super.bindObject(request, command, new PhoneInfoValidator());
if (errors.hasErrors()) {
addPhoneView.addAllObjects(errors.getModel());
return addPhoneView;
}
// otherwise --- save this object...
return addPhoneView;
}
In this way, I can easy to fix my problem when I use MultiActionController. I can bind and validate any object as I like.
thanks to ffzhuang :http://ffzhuang.blogspot.com/
- How to bind and validate in MultiActionController
- CMFCPropertyGridCtrl: How to Validate and Update Data?
- How to validate Xml Documents against schemas in BizTalk
- [WPF] How to dynamic bind Menuitem's Header and Icon
- How to Validate Network and Name Resolution Setup for the Clusterware and RAC (Doc ID 1054902.1)
- How to use git in general and bitbucket in particular
- How to bind listbox to data dynamically
- 如何在Vim中绑定滚动?(How to bind Scroll in Vim?)
- Bind: Address Already in Use Or How to Avoid this Error when Closing TCP Connections
- How to find and fix faults in Linux applications.
- How to use next and last in Perl
- how to blend VB and C# in one project
- How to call c++ exported method and classes in c#
- how to Serial COM Simply in C# and MSCOMM
- How to use plugin audio codecs in OpenH323 and OPAL
- How to Test Private and Protected methods in .NET
- How to use composition and inheritance in visual c# ?
- Memory leaks in C++ and how to avoid them
- 2011.1.17
- Java参数空异常
- 3.3 实现一种基于命令的语言
- Request获取url信息的各种方法比较
- 硬件开发的基本准则
- How to bind and validate in MultiActionController
- Spring架构设计-增强MultiActionController
- 用 MultiActionController 实现 SimpleFormController的部分功能?
- MultiActionController代理使用validator
- 什么是交叉编译?
- 匿名内部类 Anoymous Inner Class, 可以继承抽象类,实现接口
- 再谈存储过程的编写经验和优化措施、与人为本:优秀ASP.NET程序员的修炼之路
- 定时器 timer_list (一)
- 定时器 timer_list (二)