zoukankan      html  css  js  c++  java
  • SSM配置applicationContext.xml, springMVC.xml与web.xml的要点

    • springMVC.xml配置

    springMVC.xml一般主要配置Controller的组件扫描器和视图解析器

    Example:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xmlns:mvc="http://www.springframework.org/schema/mvc"
     6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
     9  
    10     <!--启动注解识别-->
    11     <context:annotation-config/>
    12  
    13     <context:component-scan base-package="com.how2java.tmall.controller">
    14         <context:include-filter type="annotation"
    15                                 expression="org.springframework.stereotype.Controller"/>
    16     </context:component-scan>
    17  
    18     <mvc:annotation-driven />
    19  
    20     <!--开通静态资源的访问-->
    21     <mvc:default-servlet-handler />
    22  
    23     <!-- 视图定位 -->
    24     <bean
    25             class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    26         <property name="viewClass"
    27                   value="org.springframework.web.servlet.view.JstlView" />
    28         <property name="prefix" value="/WEB-INF/jsp/" />
    29         <property name="suffix" value=".jsp" />
    30     </bean>
    31  
    32     <!-- 对上传文件的解析-->
    33     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
    34 </beans>
    • applicationContext.xml配置(需在web.xml文件加<listener>)

    Example:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:tx="http://www.springframework.org/schema/tx"
     5        xmlns:context="http://www.springframework.org/schema/context"
     6        xsi:schemaLocation="http://www.springframework.org/schema/context 
     7        http://www.springframework.org/schema/context/spring-context-3.0.xsd
     8         http://www.springframework.org/schema/beans 
     9      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    10      http://www.springframework.org/schema/tx 
    11      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    12 
    13     <context:annotation-config />
    14     <context:component-scan base-package="com.how2java.tmall.service" />
    15 
    16     <!-- 导入数据库配置文件 -->
    17     <context:property-placeholder location="classpath:jdbc.properties"/>
    18     <!-- 配置数据库连接池 -->
    19     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    20         <!-- 基本属性 url、user、password -->
    21         <property name="url" value="${jdbc.url}" />
    22         <property name="username" value="${jdbc.username}" />
    23         <property name="password" value="${jdbc.password}" />
    24 
    25         <!-- 配置初始化大小、最小、最大 -->
    26         <property name="initialSize" value="1" />
    27         <property name="minIdle" value="1" />
    28         <property name="maxActive" value="20" />
    29 
    30         <!-- 配置获取连接等待超时的时间 -->
    31         <property name="maxWait" value="60000" />
    32 
    33         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    34         <property name="timeBetweenEvictionRunsMillis" value="60000" />
    35 
    36         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    37         <property name="minEvictableIdleTimeMillis" value="300000" />
    38 
    39         <property name="validationQuery" value="SELECT 1" />
    40         <property name="testWhileIdle" value="true" />
    41         <property name="testOnBorrow" value="false" />
    42         <property name="testOnReturn" value="false" />
    43 
    44         <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
    45         <property name="poolPreparedStatements" value="true" />
    46         <property name="maxPoolPreparedStatementPerConnectionSize"
    47                   value="20" />
    48     </bean>
    49 
    50     <!--Mybatis的SessionFactory配置-->
    51     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
    52         <property name="typeAliasesPackage" value="com.how2java.tmall.pojo" />
    53         <property name="dataSource" ref="dataSource"/>
    54         <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    55         <!-- 分页插件 -->
    56         <property name="plugins">
    57             <array>
    58                 <bean class="com.github.pagehelper.PageInterceptor">
    59                     <property name="properties">
    60                         <value>
    61                         </value>
    62                     </property>
    63                 </bean>
    64             </array>
    65         </property>
    66     </bean>
    67 
    68     <!--Mybatis的Mapper文件识别-->
    69     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    70         <property name="basePackage" value="com.how2java.tmall.mapper"/>
    71     </bean>
    72     
    73     <!--事务管理 -->
    74     <tx:annotation-driven transaction-manager="transactionManager"/>
    75     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    76         <property name="dataSource" ref="dataSource"/>
    77     </bean>
    78 
    79 </beans>
    • web.xml文件配置

    在web.xml文件里,将springMVC.xml和applicationContext.xml一起引入

    Example:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3          xmlns="http://java.sun.com/xml/ns/javaee"
     4          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     5          version="2.5">
     6 
     7   <!-- spring的配置文件-->
     8   <context-param>
     9     <param-name>contextConfigLocation</param-name>
    10     <param-value>classpath:applicationContext.xml</param-value>
    11   </context-param>
    12   <listener>
    13     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    14   </listener>
    15 
    16   <!--中文字符编码过滤器-->
    17   <filter>
    18     <filter-name>CharacterEncodingFilter</filter-name>
    19     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    20     <init-param>
    21       <param-name>encoding</param-name>
    22       <param-value>utf-8</param-value>
    23     </init-param>
    24   </filter>
    25   <filter-mapping>
    26     <filter-name>CharacterEncodingFilter</filter-name>
    27     <url-pattern>/*</url-pattern>
    28   </filter-mapping>
    29 
    30   <!-- spring mvc核心:分发servlet -->
    31   <servlet>
    32     <servlet-name>mvc-dispatcher</servlet-name>
    33     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    34     <!-- spring mvc的配置文件 -->
    35     <init-param>
    36       <param-name>contextConfigLocation</param-name>
    37       <param-value>classpath:springMVC.xml</param-value>
    38     </init-param>
    39     <load-on-startup>1</load-on-startup>
    40   </servlet>
    41   <servlet-mapping>
    42     <servlet-name>mvc-dispatcher</servlet-name>
    43     <url-pattern>/</url-pattern>
    44   </servlet-mapping>
    45 
    46 </web-app>

     

    Written on Oct. 14th, 2019

  • 相关阅读:
    winrt组件库(包括翻书组件)
    WebRequest 获取网页乱码
    OpenNI结合Unity3D Kinect进行体感游戏开发(转)
    python(pyqt)开发环境搭建
    win8开发wpf程序遇到的无语问题
    winrt 页面进入动画
    winrt 根据类型生成实例
    性能测试基础
    性能基础入门篇
    项目部署
  • 原文地址:https://www.cnblogs.com/LittleMike/p/11672696.html
Copyright © 2011-2022 走看看