6、  spring标签 
为了使用spring标签,需要在web.xml中加入 
  <taglib> 
    <taglib-uri>/spring</taglib-uri> 
    <taglib-location>/WEB-INF/spring.tld</taglib-location> 
  </taglib> 
  
这样就可以在JSP中使用spring标签了: 
<%@ include file="/WEB-INF/jsp/include.jsp" %> 
<%@ taglib prefix="spring" uri="/spring" %> 
  
<html> 
<head><title><fmt:message key="title"/></title></head> 
<body> 
<h1><fmt:message key="priceincrease.heading"/></h1> 
<form method="post"> 
  <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5"> 
    <tr> 
      <td alignment="right" width="20%">Increase (%):</td> 
      <spring:bind path="priceIncrease.percentage"> 
        <td width="20%"> 
          <input type="text" name="percentage" value="<c:out value="${status.value}"/>"> 
        </td> 
        <td width="60%"> 
          <font color="red"><c:out value="${status.errorMessage}"/></font> 
        </td> 
      </spring:bind> 
    </tr> 
  </table> 
  <br> 
  <spring:hasBindErrors name="priceIncrease"> 
    <b>Please fix all errors!</b> 
  </spring:hasBindErrors> 
  <br><br> 
  <input type="submit" alignment="center" value="Execute"> 
</form> 
<a href="<c:url value="hello.htm"/>">Home</a> 
</body> 
</html> 
  
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的定义: 
    <bean id="priceIncreaseForm" class="web.PriceIncreaseFormController"> 
        <property name="sessionForm"><value>true</value></property> 
        <property name="commandName"><value>priceIncrease</value></property> 
        <property name="commandClass"><value>bus.PriceIncrease</value></property> 
        <property name="validator"><ref bean="priceIncreaseValidator"/></property> 
        <property name="formView"><value>priceincrease</value></property> 
        <property name="successView"><value>hello.htm</value></property> 
        <property name="productManager"> 
            <ref bean="prodMan"/> 
        </property> 
    </bean> 
  
这些定义很好理解,其中这2行: 
        <property name="commandName"><value>priceIncrease</value></property> 
        <property name="commandClass"><value>bus.PriceIncrease</value></property> 
指出了对应页面FORM的元素的类和其实例对象名。(我想不通的是为什么用command,而不是form,大概是认为提交就是命令吧) 
  
springapp-servlet.xml中关于页面导向的定义如下: 
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
        <property name="mappings"> 
            <props> 
                <prop key="/hello.htm">springappController</prop> 
                <prop key="/priceincrease.htm">priceIncreaseForm</prop> 
            </props> 
        </property> 
    </bean> 
  
下面我们看看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导向  
 
  |