2014-10-10 95 views
0

我是新来的Spring MVC和我试图理解下面的代码谁实现了Spring的BindingResult?

package com.companyname.springapp.web; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.validation.Valid; 

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 

import com.companyname.springapp.service.PriceIncrease; 
import com.companyname.springapp.service.ProductManager; 

@Controller 
@RequestMapping(value="/priceincrease.htm") 
public class PriceIncreaseFormController { 

    /** Logger for this class and subclasses */ 
    protected final Log logger = LogFactory.getLog(getClass()); 

    @Autowired 
    private ProductManager productManager; 

    @RequestMapping(method = RequestMethod.POST) 
    public String onSubmit(@Valid PriceIncrease priceIncrease, BindingResult result) 
    { 
     if (result.hasErrors()) { 
      return "priceincrease"; 
     } 

     int increase = priceIncrease.getPercentage(); 
     logger.info("Increasing prices by " + increase + "%."); 

     productManager.increasePrice(increase); 

     return "redirect:/hello.htm"; 
    } 

    @RequestMapping(method = RequestMethod.GET) 
    protected PriceIncrease formBackingObject(HttpServletRequest request) throws ServletException { 
     PriceIncrease priceIncrease = new PriceIncrease(); 
     priceIncrease.setPercentage(15); 
     return priceIncrease; 
    } 

    public void setProductManager(ProductManager productManager) { 
     this.productManager = productManager; 
    } 

    public ProductManager getProductManager() { 
     return productManager; 
    } 

} 

据我所知BindingResult是一个接口和的onsubmit方法接收BindingResult对象。我不明白的是:谁实现了这个接口来创建对象,该实现在哪里?

这里是servlet XML

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

     <bean id="productManager" class="com.companyname.springapp.service.SimpleProductManager"> 
     <property name="products"> 
      <list> 
       <ref bean="product1"/> 
       <ref bean="product2"/> 
       <ref bean="product3"/> 
      </list> 
     </property> 
     </bean> 

     <bean id="product1" class="com.companyname.springapp.domain.Product"> 
     <property name="description" value="Lamp"/> 
     <property name="price" value="5.75"/> 
     </bean> 

     <bean id="product2" class="com.companyname.springapp.domain.Product"> 
     <property name="description" value="Table"/> 
     <property name="price" value="75.25"/> 
     </bean> 

     <bean id="product3" class="com.companyname.springapp.domain.Product"> 
     <property name="description" value="Chair"/> 
     <property name="price" value="22.79"/> 
     </bean> 

     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <property name="basename" value="messages"/> 
     </bean> 

     <!-- Scans the classpath of this application for @Components to deploy as beans --> 
     <context:component-scan base-package="com.companyname.springapp.web" /> 

     <!-- Configures the @Controller programming model --> 
     <mvc:annotation-driven/> 

     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> 
     <property name="prefix" value="/WEB-INF/views/"></property> 
     <property name="suffix" value=".jsp"></property>   
     </bean> 
</beans> 

我不,如果这有助于但是我使用Tomcat 7.0,整个代码是在这里:http://docs.spring.io/docs/Spring-MVC-step-by-step/

回答