SSM整合

来源:互联网 发布:程序员所说的接口 编辑:程序博客网 时间:2024/06/05 03:24

Web.xml

配置Spring容器

  <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring/applicationContext*.xml</param-value>  </context-param> <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

解决POST乱码

<filter>        <filter-name>CharacterEncodingFilter</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>UTF-8</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>CharacterEncodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>   

配置springmvc前端控制器

<!-- springMvc的前端控制器-->    <servlet>        <servlet-name>springmvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring/springmvc.xml</param-value>        </init-param>        <!-- 屏蔽springmvc自动注册的异常处理器 -->         <init-param>            <param-name>detectAllHandlerExceptionResolvers</param-name>            <param-value>false</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>

Struts2的处理请求(如果用到)

  <filter>    <filter-name>strust2</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>strust2</filter-name>    <url-pattern>/*</url-pattern>    <dispatcher>REQUEST</dispatcher>    <dispatcher>FORWARD</dispatcher>  </filter-mapping>

Spring的配置

头文件(最好配置本地schme约束)

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

加载数据库的配置文件

<context:property-placeholder location="classpath:conf/db.properties" />    <!-- 数据库连接池 -->    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"        destroy-method="close">        <property name="url" value="${jdbc.url}" />        <property name="username" value="${jdbc.username}" />        <property name="password" value="${jdbc.password}" />        <property name="driverClassName" value="${jdbc.driver}" />        <property name="maxActive" value="10" />        <property name="minIdle" value="5" />    </bean>

spring整合sqlsessionfactory

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!-- 数据库连接池 -->        <property name="dataSource" ref="dataSource" />        <!-- 加载mybatis的全局配置文件 -->        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />    </bean>

配置mapper扫描器

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="cn.cc.mapper" />    </bean>

包扫描器

    <context:component-scan base-package="cn.cc.service" ></context:component-scan>

 事务管理器(注册事务管理器,配置通知,再到切面)

    <bean id="transactionManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <!-- 数据源 -->        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 通知 -->    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <tx:attributes>            <!-- 传播行为 -->            <tx:method name="save*" propagation="REQUIRED" />            <tx:method name="insert*" propagation="REQUIRED" />            <tx:method name="add*" propagation="REQUIRED" />            <tx:method name="create*" propagation="REQUIRED" />            <tx:method name="delete*" propagation="REQUIRED" />            <tx:method name="update*" propagation="REQUIRED" />            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />        </tx:attributes>    </tx:advice>    <!-- 切面 -->    <aop:config>        <aop:advisor advice-ref="txAdvice"            pointcut="execution(* cn.cc.service..*.*(..))" />    </aop:config>

SpringMvc配置

头文件

<?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:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

包扫描、

<context:component-scan base-package="cn.cc.controller" />

注解开启

<mvc:annotation-driven /><!--zhu jie sao miao  -->

分发控制器

<bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/" />        <property name="suffix" value="" />    </bean>
原创粉丝点击