美文网首页
Spring MVC

Spring MVC

作者: LynnGuo | 来源:发表于2018-05-21 15:01 被阅读0次

    web.xml 配置:

      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:config/spring.xml;
            classpath:config/spring-mybatis.xml;
            classpath:config/spring-shiro.xml;
            classpath:config/activiti.cfg.xml;
        </param-value>
      </context-param>
    
      <listener>
        <description>spring监听器</description>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    

    此配置是使用 ContextLoaderListener 告诉他Spring 配置文件的位置。
    监听器是 配置上下文载入器、此载入器是是载入除了DispacherServlet 载入的配置文件的其他上下文配置文件

    <servlet>
        <description>spring mvc servlet</description>
        <servlet-name>springMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <description>spring mvc 配置文件</description>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:config/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>/*</url-pattern>
      </servlet-mapping>
    

    spring-mvc.xml

        <!-- 自动扫描的包名 -->  
        <context:component-scan base-package="com.app,com.core,JUnit4" >
        </context:component-scan>  
           
            <context:component-scan base-package="sinosoft.framework; sinosoft.platform; sinosoft.project">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
            <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />
        </context:component-scan>
    
        <!-- 默认的注解映射的支持 -->  
        <mvc:annotation-driven />  
           
        <!-- 视图解释类 -->  
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
            <property name="prefix" value="/WEB-INF/jsp/"/>  
            <property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  -->  
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
        </bean>  
           
        <!-- 拦截器 -->  
        <mvc:interceptors>  
            <bean class="com.core.mvc.MyInteceptor" />  
        </mvc:interceptors>        
           
        <!-- 对静态资源文件的访问  方案一 (二选一) -->  
        <mvc:default-servlet-handler/>  
           
        <!-- 对静态资源文件的访问  方案二 (二选一)-->  
        <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>  
        <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>  
        <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>  
    
    <!-- 上传文件的配置 -->
        <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="50000000" />
    </bean>
    
        <!--国际化 -->
    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="lang" />
        </bean>
        <bean class="sinosoft.framework.core.interceptor.GlobalInterceptor" />
    </mvc:interceptors>
    

    <context:component-scan/> 扫描指定的包中的类上的注解,常用的注解有:

    @Controller 声明Action组件
    @Service 声明Service组件 @Service("myMovieLister")
    @Repository 声明Dao组件
    @Component 泛指组件, 当不好归类时.
    @RequestMapping("/menu") 请求映射
    @Resource 用于注入,( j2ee提供的 ) 默认按名称装配,@Resource(name="beanName")
    @Autowired 用于注入,(srping提供的) 默认按类型装配
    @Transactional( rollbackFor={Exception.class}) 事务管理
    @ResponseBody
    @Scope("prototype") 设定bean的作用域

    相关文章

      网友评论

          本文标题:Spring MVC

          本文链接:https://www.haomeiwen.com/subject/upbedftx.html