Spring MVC

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監(jiān)聽(tīng)器</description>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

此配置是使用 ContextLoaderListener 告訴他Spring 配置文件的位置。
監(jiān)聽(tīng)器是 配置上下文載入器、此載入器是是載入除了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

    <!-- 自動(dòng)掃描的包名 -->  
    <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>

    <!-- 默認(rèn)的注解映射的支持 -->  
    <mvc:annotation-driven />  
       
    <!-- 視圖解釋類 -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/jsp/"/>  
        <property name="suffix" value=".jsp"/><!--可為空,方便實(shí)現(xiàn)自已的依據(jù)擴(kuò)展名來(lái)選擇視圖解釋類的邏輯  -->  
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
    </bean>  
       
    <!-- 攔截器 -->  
    <mvc:interceptors>  
        <bean class="com.core.mvc.MyInteceptor" />  
    </mvc:interceptors>        
       
    <!-- 對(duì)靜態(tài)資源文件的訪問(wèn)  方案一 (二選一) -->  
    <mvc:default-servlet-handler/>  
       
    <!-- 對(duì)靜態(tài)資源文件的訪問(wèn)  方案二 (二選一)-->  
    <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>

    <!--國(guó)際化 -->
<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 泛指組件, 當(dāng)不好歸類時(shí).
@RequestMapping("/menu") 請(qǐng)求映射
@Resource 用于注入,( j2ee提供的 ) 默認(rèn)按名稱裝配,@Resource(name="beanName")
@Autowired 用于注入,(srping提供的) 默認(rèn)按類型裝配
@Transactional( rollbackFor={Exception.class}) 事務(wù)管理
@ResponseBody
@Scope("prototype") 設(shè)定bean的作用域

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評(píng)論 6 342
  • 1、Spring MVC請(qǐng)求流程 (1)初始化:(對(duì)DispatcherServlet和ContextLoderL...
    拾壹北閱讀 2,018評(píng)論 0 12
  • 1.1 spring IoC容器和beans的簡(jiǎn)介 Spring 框架的最核心基礎(chǔ)的功能是IoC(控制反轉(zhuǎn))容器,...
    simoscode閱讀 6,851評(píng)論 2 22
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong閱讀 22,942評(píng)論 1 92

友情鏈接更多精彩內(nèi)容