zoukankan      html  css  js  c++  java
  • SpringMVC控制器配置文件

    1 首先引入 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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    2 <!-- 扫描指定包目下所有java类,把标记了@Controller注解的类转换为bean -->     
    <context:component-scan base-package="com.mvc.spring.web" />
    
    3   <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 ,有几种形式-->
    A    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"     
              p:prefix="/WEB-INF/view/" p:suffix=".jsp" />  
    B     <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
             <property name="prefix" value="/WEB-INF/view/" />
         <property name="suffix" value=".jsp"  />
         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
           </bean> 
    分析  viewClass指定了选择何种视图技术,这里使用JSTL技术;
          prefix是视图文件的前缀,即目录名,因为我们把persons.jsp放到了目录/WebRoot/之下,所以只需要配置一个“/”,如果放在目录/WebRoot/目录下的一个名称为view的目录中,这里prefix的值应该为/view/;
          suffix是视图文件的后缀,即扩展名,如使用JSP文件,则为“.jsp”。
     
    4  配置servelt上的注解映射
    <!-- 配置处理在类级别上的@RequestMapping注解,允许扩展自定义拦截器,在此类的基础上,只需要设置interceptors参数 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <!-- 配置处理方法级别上的@RequestMapping注解 ,允许扩展自定义的方法注解,例如参数 webBindingInitializer,customArgumentResolver,messageConverters-->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    
    5  <!-- 表示使用cglib,而非JDK的动态代理,因为Controller没有实现接口,所以要配置这里 -->
    <aop:aspectj-autoproxy proxy-target-class="true"  />
    
    6  <!--用于文件上传->
     <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver"     
              p:defaultEncoding="utf-8"   p:resolveLazily="ture" />    
     
      <!---通过该对象能绑定数据验证[自定义也行]与新的ConversionService->
    7 <bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
        <property name="validator" ref="validator" />
        <property name="conversionService" ref="conversionService"/>  
     </bean>
    
      <!---自定义的错误消息文件和错误消息键值,配置ValidationMessages.properties,此时错误消息键值的查找会先到classpath下ValidationMessages.properties中找,找不到再到默认的错误消息文件中找。->
    8 <bean id="validator"   class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">  
           <property name="providerClass"   value="org.hibernate.validator.HibernateValidator"/>  
    </bean>  
    
       <!--jsr303 数据验证,用于验证注解方法上的参数合法性,为了不影响业务代码,提前在执行前验证,因为版本较高,需要hibernate4[已经实现]-->
    9 <bean id="methodValidationPostProcessor" class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor">
           <property name="validator" ref="validator"></property>
       </bean>
    
    API(简称,全称,作用)
    A InternalResourceViewResolver  [视图解析器]
    org.springframework.web.servlet.view.InternalResourceViewResolver
    网络解析点  :http://7-sun.com/doc/spring2.5_doc_cn/org/springframework/web/servlet/view/InternalResourceViewResolver.html
    
    B  JstlView [JSTL视图]
    org.springframework.web.servlet.view.JstlView
    
    C DefaultAnnotationHandlerMapping [映射控制器类上的注解]
    org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
    
    D AnnotationMethodHandlerAdapter  [映射控制器方法上的注解]
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
    
    E CommonsMultipartResolver [文件上传]
    org.springframework.web.multipart.commons.CommonsMultipartResolver
    
    F ConfigurableWebBindingInitializer [设置(绑定)某些重要属性spring属性]
    org.springframework.web.bind.support.ConfigurableWebBindingInitializer
    
    G LocalValidatorFactoryBean [配置ValidationMessages.properties]
    org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
    
    J MethodValidationPostProcessor  [方法参数验证[jsr303]]
    org.springframework.validation.beanvalidation.MethodValidationPostProcessor
  • 相关阅读:
    LA 2038 Strategic game(最小点覆盖,树形dp,二分匹配)
    UVA 10564 Paths through the Hourglass(背包)
    Codeforces Round #323 (Div. 2) D 582B Once Again...(快速幂)
    UVALive 3530 Martian Mining(贪心,dp)
    UVALive 4727 Jump(约瑟夫环,递推)
    UVALive 4731 Cellular Network(贪心,dp)
    UVA Mega Man's Mission(状压dp)
    Aizu 2456 Usoperanto (贪心)
    UVA 11404 Plalidromic Subsquence (回文子序列,LCS)
    Aizu 2304 Reverse Roads(无向流)
  • 原文地址:https://www.cnblogs.com/ak23173969/p/4980637.html
Copyright © 2011-2022 走看看