Spring起步例子 “Developing a Spring Framework MVC application step-by-step” 剖析(三)

来源:百度文库 编辑:神马文学网 时间:2024/04/17 03:46:53
 



6、  spring标签

为了使用spring标签,需要在web.xml中加入

 

    /spring

    /WEB-INF/spring.tld

 

 

这样就可以在JSP中使用spring标签了:

<%@ include file="/WEB-INF/jsp/include.jsp" %>

<%@ taglib prefix="spring" uri="/spring" %>

 

<fmt:message key="title"/>

 

   

     

     

       

       

     

   

 

Increase (%):

          ">

       

         

       

 

 

    Please fix all errors!

 

 

 

">Home

 

7、  SimpleFormController的运作机制

前面说的Controller,比如SpringappController,它们只行使了页面流程控制和model的产生,都与页面的Form无关,也就是说不接受来自页面的提交数据。

为了处理页面的提交数据,那要让Controller继承自SimpleFormController。

通常的SimpleFormController定义如下:

public class PriceIncreaseFormController extends SimpleFormController {

    private ProductManager prodMan;

 

    public ModelAndView onSubmit(Object command)

            throws ServletException {

        int increase = ((PriceIncrease) command).getPercentage();

 

        prodMan.increasePrice(increase);

 

        String now = (new java.util.Date()).toString();

 

        Map myModel = new HashMap();

        myModel.put("now", now);

        myModel.put("products", getProductManager().getProducts());

 

        return new ModelAndView(new RedirectView(getSuccessView()));

    }

 

    protected Object formBackingObject(HttpServletRequest request) throws ServletException {

        PriceIncrease priceIncrease = new PriceIncrease();

        priceIncrease.setPercentage(20);

 

        return priceIncrease;

    }

 

    public void setProductManager(ProductManager pm) {

        prodMan = pm;

    }

    public ProductManager getProductManager() {

        return prodMan;

    }

}

 

跟Controller的区别在于多了2个方法:

public ModelAndView onSubmit(Object command)

protected Object formBackingObject(HttpServletRequest request) throws ServletException

 

在理解SimpleFormController之前,我们先看看springapp-servlet.xml中关于SimpleFormController的定义:

   

        true

        priceIncrease

        bus.PriceIncrease

       

        priceincrease

        hello.htm

       

           

       

   

 

这些定义很好理解,其中这2行:

        priceIncrease

        bus.PriceIncrease

指出了对应页面FORM的元素的类和其实例对象名。(我想不通的是为什么用command,而不是form,大概是认为提交就是命令吧)

 

springapp-servlet.xml中关于页面导向的定义如下:

   

       

           

                springappController

                priceIncreaseForm

           

       

   

 

下面我们看看SimpleFormController的运作机制:

由于它也是一个Controller,所以它的加载机制同其他的Controller。比如也自动初始化了成员属性。

 

当页面请求交给PriceIncreaseFormController处理的时候,它首先调用的是formBackingObject()方法,其作用是加载页面Form元素对应的bean,并赋初始值。完成之后,就产生页面显示。

 

当用户提交FORM时,servlet不是先运行PriceIncreaseFormController. onSubmit(),而是先运行了priceIncreaseValidator的support()和validate (Object obj, Errors errors)方法。如果在校验过程中(即validate (Object obj, Errors errors)方法中),如果发现有数据错误,那么就errors.rejectValue()方法给errors赋值。〔〔errors.rejectValue()方法待研究〕〕

servlet一旦发现errors里面有值,就会中止程序运行而直接返回原来的页面,在页面的某处可以利用errors里面存储的值显示错误信息。

如果校验通过,那么SimpleFormController的onSubmit方法将启动,其返回值就是将要去的页面。

 

总结一个SimpleFormController的运作全过程:

servlet请求->SimpleFormController.formBackingObject()->显示页面->提交页面-> SimpleFormController的Validator.validate()->SimpleFormController. onSubmit()->view导向