<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    推薦淘寶秋冬男裝熱賣網(wǎng)店

    追求無止境

    我的程序人生
    隨筆 - 31, 文章 - 2, 評論 - 20, 引用 - 0
    數(shù)據(jù)加載中……

    Spring MVC 框架學習筆記之BaseCommandController和AbstractCommandController

     

    Spring的BaseCommandController繼承自AbstractController。在看BaseCommandController之前先看他的繼承類AbstractCommandController是如何實現(xiàn)

    AbstractController的handleInternalRequest方法的:

    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    Object command = getCommand(request);
    ServletRequestDataBinder binder = bindAndValidate(request, command);
    BindException errors = new BindException(binder.getBindingResult());
    return handle(request, response, command, errors);
    }
    

    getCommand就是BaseCommandController中的方法。

    protected Object getCommand(HttpServletRequest request) throws Exception {
    return createCommand();
    }
    protected final Object createCommand() throws Exception {
    if (this.commandClass == null) {
    throw new IllegalStateException("Cannot create command without commandClass being set - " +
    "either set commandClass or (in a form controller) override formBackingObject");
    }
    if (logger.isDebugEnabled()) {
    logger.debug("Creating new command of class [" + this.commandClass.getName() + "]");
    }
    return BeanUtils.instantiateClass(this.commandClass);
    }
    

    createCommand創(chuàng)建了一個CommandClass的對象。

    然后再看bindAndValidate方法:

    protected final ServletRequestDataBinder bindAndValidate(HttpServletRequest request, Object command)
    throws Exception {
    ServletRequestDataBinder binder = createBinder(request, command);
    BindException errors = new BindException(binder.getBindingResult());
    if (!suppressBinding(request)) {
    binder.bind(request);
    onBind(request, command, errors);
    if (this.validators != null && isValidateOnBinding() && !suppressValidation(request, command, errors)) {
    for (int i = 0; i < this.validators.length; i++) {
    ValidationUtils.invokeValidator(this.validators[i], command, errors);
    }
    }
    onBindAndValidate(request, command, errors);
    }
    return binder;
    }
    

    這個方法首先創(chuàng)建了 DataBinder對象,然后,獲取創(chuàng)建綁定對象時發(fā)生的錯誤。報錯在errors。接下來綁定對象,調(diào)用onBind處理綁定事件;接下來應用Validator。然后調(diào)用onBindAndValidate來處理綁定和驗證事件。最后返回binder。

    處理完之后調(diào)用handle方法進行處理。

    綜上所述,AbstractCommandController具有兩個功能:

    1、將請求參數(shù)轉(zhuǎn)換為Command對象。在該Controller中,我們設置一個object對象。然后BaseCommandController將請求的參數(shù)進行轉(zhuǎn)換。如果請求參數(shù)有value值,就會調(diào)用object的的setValue對象來設置對象里的值。如果請求參數(shù)中有address.city.就會調(diào)用object中getAddress().setCity()方法來賦值。這個object可以是任意的object,唯一的要求就是這個object類沒有參數(shù)。

    2、對數(shù)據(jù)進行驗證。在轉(zhuǎn)換和驗證時發(fā)生錯誤時,需要在handle(request, response, command, errors)中進行處理。

    posted on 2009-11-25 16:25 追求無止境 閱讀(5395) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 亚洲人成网站在线播放2019 | 亚洲国产高清精品线久久| 男人的天堂av亚洲一区2区| 亚洲国产中文v高清在线观看| 久久免费视频网站| 亚洲精品永久在线观看| 国产AV无码专区亚洲AV手机麻豆| 免费A级毛片无码专区| 亚洲av永久无码| 精品亚洲综合在线第一区| 国产免费AV片在线播放唯爱网| 国产成人精品日本亚洲语音| 久久精品国产亚洲AV麻豆不卡| 成人av免费电影| 七色永久性tv网站免费看| 亚洲成在人线aⅴ免费毛片| 亚洲AV成人一区二区三区AV| 国产青草视频免费观看97| 男人进去女人爽免费视频国产| 亚洲性无码一区二区三区| 亚洲AV无码码潮喷在线观看| 日本免费人成黄页网观看视频| 人人玩人人添人人澡免费| 国产偷国产偷亚洲高清在线| 亚洲第一香蕉视频| 久久久久亚洲av毛片大| 毛片网站免费在线观看| 嫩草成人永久免费观看| 免费无遮挡无遮羞在线看| 亚洲午夜电影在线观看高清 | 亚洲高清在线视频| 四虎影视精品永久免费网站| 亚洲一区二区三区免费观看| 国产精品免费久久久久影院| 久久亚洲色WWW成人欧美| 在线免费观看亚洲| 亚洲国产精华液网站w| 亚洲精品无码久久毛片| 免费看a级黄色片| 国产免费AV片在线播放唯爱网| 日韩在线永久免费播放|