springMVC 之 AbstractFormController

本文详细解析了Spring MVC框架中的AbstractFormController类。包括其构造方法中的配置选项含义,如设置表单会话范围、绑定验证行为及命令名称等;介绍了formBackingObject()方法如何初始化表单数据;解释了onBindAndValidate()方法如何实现表单数据的验证;并说明了referenceData()方法如何为JSP页面提供引用数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
2007-08-25 09:55:28
转载: 以下所有资料由程序自动摘自 http://www.youkuaiyun.com
public class AccountFormController extends SimpleFormController {

        public static final String[] LANGUAGES = {"english", "japanese"};

        private PetStoreFacade petStore;

        public AccountFormController() {
                setSessionForm(true);
                setValidateOnBinding(false);
                setCommandName("accountForm");
                setFormView("newUser");
        }

        public void setPetStore(PetStoreFacade petStore) {
                this.petStore = petStore;
                System.out.println(this.getClass()+"this.petStore = petStore;");
        }

        protected Object formBackingObject(HttpServletRequest request) throws Exception {
                UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request, "userSession");
                if (userSession != null) {
                        return new AccountForm(this.petStore.getUser(userSession.getUsers().getId().intValue()));
                }else{
                        return new AccountForm();
                }

        }

        protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
                        throws Exception {
                AccountForm accountForm = (AccountForm) command;
                Users users = accountForm.getUsers();
                errors.setNestedPath("users");
                getValidator().validate(users, errors);
                errors.setNestedPath("");

                if(accountForm.isNewAccount()) {

                 ValidationUtils.rejectIfEmpty(errors, "users.username", "USER_ID_REQUIRED", "User ID is required.");
                 if(users.getPassword()==null|| users.getPassword().length() < 1 ||
                                        !users.getPassword().equals(accountForm.getRepeatedPassword())) {
                         errors.reject("PASSWORD_MISMATCH","Passwords did not match or were not provided. Matching passwords are required.");
                  }
                }else if (users.getPassword() != null && users.getPassword().length()>0){
                  if(!users.getPassword().equals(accountForm.getRepeatedPassword()))
                  {
                      errors.reject("PASSWORD_MISMATCH","Passwords did not match. Matching passwords are required.");
                  }
               }
         }

        protected Map referenceData(HttpServletRequest request) throws Exception {
             Map model = new HashMap();
             model.put("languages", LANGUAGES);
             return model;
        }

        protected ModelAndView onSubmit(
                       HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
                        throws Exception {

                AccountForm accountForm = (AccountForm) command;

                try {
                        if(accountForm.isNewAccount())
                        {
                                this.petStore.insertUsers(accountForm.getUsers());
                        }else{
                                this.petStore.updateUsers(accountForm.getUsers());
                        }
                }catch (DataIntegrityViolationException ex){
                        errors.rejectValue("account.username","USER_ID_ALREADY_EXISTS","User ID already exists: choose a different ID.");
                        return showForm(request, response, errors);
                }

                UserSession userSession = new UserSession(this.petStore.getUser(accountForm.getUsers().getId().intValue()));
                request.getSession().setAttribute("userSession", userSession);
                return super.onSubmit(request, response, command, errors);
        }
在以上该formController中
能帮我解释一下:
1、 
setSessionForm(true);
setValidateOnBinding(false);
setCommandName("accountForm");
这三句是干什么用的有什么左右??
2、
formBackingObject()该方法有什么作用?
3、
onBindAndValidate()该方法有什么作用?
4、
referenceData()该方法有什么作用?
 
----------------------------------------------------------------
搂主看得是springMVC的petstore例子吧。如果你理解了springMVC,相信这3句话很好理解。
1
setSessionForm(true); //使form在session范围内有效
setValidateOnBinding(false);//是否绑定了验证
setCommandName("accountForm");//你的commandName是这个accountForm,相当于struts的form
2
formBackingObject()方法是回传数据到表单,最后返回的是Command对象
3,onBindAndValidate(),当你表单的数据已经绑定上,但还没开始验证之前,你可以在这个方法中作一些预处理。
4,referenceData()方法是方便你在JSP页面对一些数据做出调用的。
比如你方法当中
protected Map referenceData(HttpServletRequest request) throws Exception {
             Map model = new HashMap();
             model.put("languages", LANGUAGES);
             return model;
        }
jsp便可从页面${languages},获得你绑定的值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值